Kubernetes: emptying the finalizers for a namespace that will not delete

If your intent is to delete all the objects in a namespace, but the command is not completing, emptying the namespace finalizer will often allow the deletion to finish.

For example, if you have tried deleting the “my-namespace” like below and it will not complete.

kubectl delete ns my-namespace --force --grace-period=0

Then as written by Ushakov Roman on stackoverflow,  use jq to apply an empty finalizer to the kubectl replace command.

kubectl get ns my-namespace -o json | jq '.spec.finalizers = []' | kubectl replace --raw "/api/v1/namespaces/my-namespace/finalize" -f -

If you need to install jq, that can be done with apt on Ubuntu.

# install 'jq' utility
sudo apt install -y jq

 

REFERENCES
stackoverflow, how to force delete a kubernetes namespace

github jq

NOTES

clearing finalizer for an object [link]

kubectl patch configmap/mymap \
    --type json \
    --patch='[ { "op": "remove", "path": "/metadata/finalizers" } ]'