Kubectl: Force Delete Namespace Stuck In Terminating

Sometimes a deletion of a Namespace in Kubernetes gets hung up and the kubectl delete namespace command never completes.

Such Namespaces get stuck in a Terminating state but can be manually deleted using a Kubernetes API.

This note shows how to force delete the Terminating Namespaces in Kubernetes.

Force Delete Terminating Namespace in Kubernetes

List the Namespaces that are stuck in the Terminating state:

$ kubectl get ns
- sample output -
NAME                 STATUS          AGE
<namespaceName>      Terminating     10d

Create a temporary JSON file representing the Terminating Namespace:

$ kubectl get ns <namespaceName> -o json > tmp.json

Edit your tmp.json file:

$ vi tmp.json

Remove any values from the spec.finalizers field:

…
    "spec": {
        "finalizers": [
        ]
    },
…

It should look as follows:

Save the file and start an HTTP proxy to access the Kubernetes API:

$ kubectl proxy
- sample output -
Starting to serve on 127.0.0.1:8001

Keep the command above running and start a new terminal session.

Run the following API call from the new terminal window:

$ curl -k -H "Content-Type: application/json" \
          -X PUT \
          --data-binary @tmp.json \
          http://127.0.0.1:8001/api/v1/namespaces/<namespaceName>/finalize

Verify that the Terminating Namespace is removed:

$ kubectl get ns <namespaceName>
- sample output -
Error from server (NotFound): namespaces "<namespaceName>" not found

Leave a Reply