Search code examples
kuberneteskubernetes-helm

Delete Kubernetes namespace only if it's empty?


I am using Helm to deploy multiple "components" of my application into a single namespace and using Jenkins to trigger create and destroy jobs. It doesn't seem that I can use Helm to delete the namespace thus I am looking to just use a Kubernetes command.

However, It seems that if I use kubectl delete namespace it will forcefully destroy the namespace and all its resources.

I'd like to destroy the namespace only if it is empty. Is there a command to do this?


Solution

  • See Remove Empty Namespaces Operator, it can do exactly what you want.

    Why? Because it's not so easy to iterate over resources in the namespace to decide if it's empty or not. After all, there are "default resources" like default service account and probably other stuff from you tooling/operators.

    So these resources should be excluded from iteration. Bash scripting becomes too complicated this way. And one day I decided to implement it with Python.