DEV Community

Joseph D. Marhee
Joseph D. Marhee

Posted on

How to delete a Kubernetes namespace stuck at Terminating Status

After running a command like:

kubectl delete my-namespace
Enter fullscreen mode Exit fullscreen mode

and the command hangs indefinitely after accepting the deletion request, you might find it sitting at Terminating:

$ kubectl get ns/my-namespace
NAME      STATUS         AGE
testing   Terminating    113m
Enter fullscreen mode Exit fullscreen mode

This might be due to a Finalizer in the spec, specific conditions to be met during deletion. You can view the finalizers for your namespace:

...
    "spec": {
       "finalizers": [
           "kubernetes"
       ]
    }
...
Enter fullscreen mode Exit fullscreen mode

This is what a standard namespace with no other conditions might otherwise look like, so if it's hanging, this is likely way.

You can modify the resource using kubectl edit ns/my-namespace, and replace the finalizer with an empty array:

...
    "spec": {
       "finalizers": [
       ]
    }
...
Enter fullscreen mode Exit fullscreen mode

or use the Kube API to update the specification to allow it to finalize manually if this is unsuccessful by dumping the resource:

kubectl get ns/my-namespace -o json > my-namespace.json
Enter fullscreen mode Exit fullscreen mode

and editing that my-namespace.json to remove the finalizer, and then making a PUT request to the API:

kubectl replace --raw "/api/v1/namespaces/my-namespace/finalize" -f ./my-namespace.json
Enter fullscreen mode Exit fullscreen mode

or using an HTTP client to issue the request (usually will require authentication if not exposing via kubectl proxy) using that same json dump:

curl -k -H "Content-Type: application/json" -X PUT --data-binary @my-namespace.json "https://<Kube API Endpoint>/api/v1/namespaces/my-namespace/finalize"
Enter fullscreen mode Exit fullscreen mode

You can then confirm, once you receive a successful response, that the namespace is deleted:

kubectl get ns/my-namespace
Enter fullscreen mode Exit fullscreen mode

which should return no resource.

Top comments (0)