How do I force delete Namespaces stuck in Terminating?
Steps to recreate:
- Apply this YAML
apiVersion: v1
kind: Namespace
metadata:
name: delete-me
spec:
finalizers:
- foregroundDeletion
kubectl delete ns delete-me
It is not possible to delete
delete-me
.
The only workaround I've found is to destroy and recreate the entire cluster.
Things I've tried:
None of these work or modify the Namespace. After any of these the problematic finalizer still exists.
Edit the YAML and kubectl apply
Apply:
apiVersion: v1
kind: Namespace
metadata:
name: delete-me
spec:
finalizers:
$ kubectl apply -f tmp.yaml
namespace/delete-me configured
The command finishes with no error, but the Namespace is not udpated.
The below YAML has the same result:
apiVersion: v1
kind: Namespace
metadata:
name: delete-me
spec:
kubectl edit
kubectl edit ns delete-me
, and remove the finalizer. Ditto removing the list entirely. Ditto removing spec
. Ditto replacing finalizers
with an empty list.
$ kubectl edit ns delete-me
namespace/delete-me edited
This shows no error message but does not update the Namespace. kubectl edit
ing the object again shows the finalizer still there.
kubectl proxy &
kubectl proxy &
curl -k -H "Content-Type: application/yaml" -X PUT --data-binary @tmp.yaml http://127.0.0.1:8001/api/v1/namespaces/delete-me/finalize
As above, this exits successfully but does nothing.
Force Delete
kubectl delete ns delete-me --force --grace-period=0
This actually results in an error:
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
Error from server (Conflict): Operation cannot be fulfilled on namespaces "delete-me": The system is ensuring all content is removed from this namespace. Upon completion, this namespace will automatically be purged by the system.
However, it doesn't actually do anything.
Wait a long time
In the test cluster I set up to debug this issue, I've been waiting over a week. Even if the Namespace might eventually decide to be deleted, I need it to be deleted faster than a week.
Make sure the Namespace is empty
The Namespace is empty.
$ kubectl get -n delete-me all
No resources found.
etcdctl
$ etcdctl --endpoint=http://127.0.0.1:8001 rm /namespaces/delete-me
Error: 0: () [0]
I'm pretty sure that's an error, but I have no idea how to interpret that. It also doesn't work. Also tried with --dir
and -r
.
ctron/kill-kube-ns
There is a script for force deleting Namespaces. This also does not work.
$ ./kill-kube-ns delete-me
Killed namespace: delete-me
$ kubectl get ns delete-me
NAME STATUS AGE
delete-me Terminating 1h
POST
ing the edited resource to /finalize
Returns a 405. I'm not sure if this is the canonical way to POST to /finalize though.
Links
This appears to be a recurring problem and none of these resources helped.
kubectl -n <ns> get all -o yaml
maybe it gives you some more info. – Agroundmicrok8s v1.26.0 on Ubuntu 22.04
. The answers work, but it would be nice to know (+ fix) the actual root cause. – Illuminant