How to delete a Kubernetes namespace stuck at Terminating Status
Joseph D. Marhee
Posted on August 17, 2022
After running a command like:
kubectl delete my-namespace
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
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"
]
}
...
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": [
]
}
...
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
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
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"
You can then confirm, once you receive a successful response, that the namespace is deleted:
kubectl get ns/my-namespace
which should return no resource.
Posted on August 17, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.