Delete Kubernetes namespace stuck in terminating status

Dhruv | Oct 13, 2022

Ahh! I can’t delete this namespace. Why is it stuck in Terminating stage since forever. If you are facing this, there is an easy way out. Just follow the steps!

Let’s say our namespace is MY_NAMESPACE

Get the namespace json by running kubectl get ns/MY_NAMESPACE -o json. You will find a section with finalizers.

"spec": {
        "finalizers": [

Now what does a finalizer do? A finalizer is a unique metadata key that instructs Kubernetes to hold off on fully deleting a resource unless a certain condition is satisfied. Therefore, Kubernetes looks for a finalizer in the metadata.finalizers field when you run a command like kubectl remove namespace abcd. The namespace is not destroyed if the resource specified in the finalizer cannot be deleted for any reason. As a result, the namespace enters a terminating state and waits for the resource removal, which never happens. Check an object’s finalizers by looking at the metadata.finalizers field in its YAML when it has been terminating for an excessive amount of time.

The next steps are easy. Edit the json and empty the finalizers array so it looks like:

"spec": {
        "finalizers": []

Save it as MY_NAMESPACE.json(you can save it by any name). Now you have to update the namespace configuration of MY_NAMESPACE.

Run the following command to update the namespace configuration:

kubectl replace --raw "/api/v1/namespaces/MY_NAMESPACE/finalize" -f ./MY_NAMESPACE.json

Voila!!!! Your namespace will be deleted successfully. You can run kubectl get ns to verify.

Hopefully this sorts your namespace stuck woes.