How to remove namespace stuck in Terminating state in Kubernetes
1 min read
Having trouble deleting a Kubnernetes namespace? It might be stuck in a Terminating
state. Here’s how to identify and fix the issue.
$ kubectl delete namespace test-namepspacenamespace "test-namepspace" deleted
$ kubectl get namespaceNAME STATUS AGEdefault Active 69dingress-nginx Active 69dkube-node-lease Active 69dkube-public Active 69dkube-system Active 69dkubernetes-dashboard Active 69dtest-namespace Terminating 2d
- Dump the contents of the namespace to a temporary file:
kubectl get namespace test-namespace -o json > tmp.json
- Edit the temporary file in your favourite editor
vim tmp.json
- Remove
"kubernetes"
from the finalizer array and save the file
{ "apiVersion": "v1", "kind": "Namespace", "metadata": { "creationTimestamp": "2023-04-16T12:00:00Z", "deletionTimestamp": "2023-04-16T12:00:00Z", "name": "test-namespace", "resourceVersion": "123456", "selfLink": "/api/v1/namespaces/test-namespace", "uid": "123456" }, "spec": { "finalizers": [ "kubernetes" ] }, "status": { "phase": "Terminating" }}
- Start the proxy (if you haven’t already)
kubectl proxy
- Call the Kubernetes API application/json against the /finalize endpoint for the namespace to update the JSON. Use the port number appropriate for your instance.
curl -k -H "Content-Type: application/json" -X PUT --data-binary @tmp.json http://localhost:8001/api/v1/namespaces/test-namespace/finalize
- Check the namespace status
$ kubectl get namespace test-namespaceError from server (NotFound): namespaces "test-namespace" not found
References: