<p>While I don't know of any operator that allows for this directly, it can be done with a bash one liner with <code>awk</code>, <code>xargs</code>, <code>sed</code>, and gnu <code>date</code>. I've taken it from <a href="https://stackoverflow.com/questions/48934491/kubernetes-how-to-delete-pods-based-on-age-creation-time">this  post</a> and modified it to do namespaces, and to not delete the <code>default</code> or <code>kube-*</code> namespaces.</p>

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

<p>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.</p>

CC BY-SA 4.0
2019-01-14T19:59:44.877Z
2019-01-14T19:59:44.877Z
Marcus
<p>I recommend that you have an expire-date as label or <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/" rel="nofollow noreferrer">annotation</a> on the namespaces used for test. Then you can have a <a href="https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/" rel="nofollow noreferrer">Job</a> to clean-up expired namespaces.</p>

CC BY-SA 4.0
2019-01-14T20:43:35.843Z
2019-01-14T20:43:35.843Z
Jonas
<p>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?</p>

<kubernetes>
How to automatically remove k8s namespaces every week?
030
# How to automatically remove k8s namespaces every week?

(Asked by: username_0 on 2019-01-12)

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?

**Tags:** kubernetes


---

## 2 answers

### Answer by username_2 on 2019-01-14. Score: 3 | Accepted answer

I recommend that you have an expire-date as label or [annotation](https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/) on the namespaces used for test. Then you can have a [Job](https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/) to clean-up expired namespaces.


---

### Answer by username_1 on 2019-01-14. Score: 1

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.


---

