In our company multiple stakeholders create test systems in k8s that they forget to remove. The aim is to remove namespaces that are older than one week automatically. Is there a certain operator that is able to do this?

2 Answers 2


I recommend that you have an expire-date as label or annotation on the namespaces used for test. Then you can have a Job to clean-up expired namespaces.


While I don't know of any operator that allows for this directly, it can be done with a bash one liner with awk, xargs, sed, and gnu date. I've taken it from this post and modified it to do namespaces, and to not delete the default or kube-* namespaces.

kubectl get namespaces -o go-template --template '{{range .items}}{{.metadata.name}} {{.metadata.creationTimestamp}}{{"\n"}}{{end}}' | awk '$2 <= "'$(date -d '1 week ago' -Ins --utc | sed 's/+0000/Z/')'" && $1 !~ /(default|kube.*)/ { print $1 }' | xargs --no-run-if-empty kubectl delete namespace

Be careful using this though, it's worth running it without the final xargs call to verify that it is selecting the namespaces you are interested in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.