[Solved] Kubernetes Namespace caught in Terminating state



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

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

This can forestall the namespace from being eliminated.

Learn how to discover sources that have to be deleted

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

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

This can run by all API Sources accessible 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 need to use the next 1-liner Python script.

Be sure to switch “” 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();knowledge = json.load(p.stdout);knowledge['spec']['finalizers'] = [];requests.put('{}/finalize'.format(namespace), json=knowledge).raise_for_status()"