DEV Community

Joseph D. Marhee
Joseph D. Marhee

Posted on

3

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.

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more