[Solved] Kubernetes Namespace caught in Terminating state



You probably have tried to delete a Kubernetes namespace, and it has been hanging in ‘deleting’ for hours on finish, it’s doubtless that you’ve got dangling assets that haven’t deleted but.

$ kubectl get ns
NAME              STATUS        AGE
apps              Lively        2d19h
default           Lively        3d8h
my-apps           Terminating   11h

It will forestall the namespace from being eliminated.

How one can discover assets that must be deleted

You may chain the api-resources verbs together with a kubectl get to search out dangling assets:

kubectl api-resources --verbs=checklist --namespaced -o identify | xargs -n 1 kubectl get --show-kind --show-labels --ignore-not-found -n <your_namespace>

It will run by all API Assets out there in your cluster and report if they’re present in that namespace.

Now you’ll be able to merely go and kubectl delete <useful resource> to every one as you discover them.

A 1-liner to delete the issue namespace

If you’re nonetheless not capable of delete the namespace, then you should use the next 1-liner Python script.

Make sure that to exchange “” together with your namespace itself:

python3 -c "namespace='<my-namespace>';import atexit,subprocess,json,requests,sys;proxy_process = subprocess.Popen(['kubectl', 'proxy']);atexit.register(proxy_process.kill);p = subprocess.Popen(['kubectl', 'get', 'namespace', namespace, '-o', 'json'], stdout=subprocess.PIPE);p.wait();information = json.load(p.stdout);information['spec']['finalizers'] = [];requests.put('{}/finalize'.format(namespace), json=information).raise_for_status()"