How to Force‑Delete a Stuck Kubernetes Namespace (Step‑by‑Step Guide)
This guide explains why a Kubernetes namespace may remain in a Terminating state, shows how to inspect resources, edit the namespace JSON to remove finalizers, use the Kubernetes API via curl with a proxy, and verify that the namespace is finally removed.
Problem Description
During testing a pod could not start because its namespace stayed in Terminating status.
kubectl get ns
NAME STATUS AGE
configmap Terminating 135d
default Active 207d
harbor Active 207d
kube-flannel Terminating 17m
kube-node-lease Active 207d
kube-public Active 207d
kube-system Active 207d
kubekey-system Active 207d
kubernetes-dashboard Active 207d
local-path-storage Active 187d
nginx Active 146d
test Terminating 126dEven using kubectl delete ns test --force leaves the namespace stuck.
Inspecting Resources in the Namespace
No resources are found in the test namespace:
# kubectl get all -n test
No resources found in test namespace.Additional checks with
kubectl api-resources -o name --verbs=list --namespaced | xargs -n 1 kubectl get --show-kind --ignore-not-found -n testalso return nothing.
Resolution Steps
1. Export the namespace definition to JSON: # kubectl get ns test -o json > test.json 2. Edit test.json and ensure the spec section is empty, removing the finalizers array:
{
"spec": {}
}3. Use the Kubernetes API to finalize the namespace (the API server requires a proxy on port 8081):
# kubectl proxy --port=8081
Starting to serve on 127.0.0.1:80814. Send a PUT request with the cleaned JSON:
curl -k \
-H "Content-Type: application/json" \
-X PUT \
--data-binary @test.json \
http://127.0.0.1:8081/api/v1/namespaces/test/finalizeThe response confirms the namespace is finalized and still in Terminating phase, but with no remaining finalizers.
Verify Deletion
# kubectl get ns
NAME STATUS AGE
configmap Terminating 135d
default Active 207d
harbor Active 207d
kube-flannel Terminating 21m
kube-node-lease Active 207d
kube-public Active 207d
kube-system Active 207d
kubekey-system Active 207d
kubernetes-dashboard Active 207d
local-path-storage Active 187d
nginx Active 146dThe test namespace has been removed. The same method works for other namespaces stuck in Terminating . Note: avoid naming a namespace the same as a resource type (e.g., configmap) because it can cause the API to misinterpret the name.
For more details see the original article:
(© Original author, removed upon request)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
