Kubernetes Namespace Stuck in Terminating State

Often the deletion of a namespace is required and using the well-known command to delete kubernetes namespace

kubectl delete ns <namsepace_name>

puts the namespace in Terminating state and it gets stuck there. Waiting for hours, even days won’t help in this regard unless the finalizers are deleted and the manifest has been replaced via kubernetes API.

Fix

To fix namespace delete stuck in terminating state. We get the namespace manifest and save it as a JSON file.


NOTE

Change namespace_name to the namespace name that is in a terminating state.


kubectl get ns namespace_name -o json > namesapce_name.json

Once we have the JSON file, edit the file to delete the kubernetes finalizers.

sed -i -e 's/"kubernetes"//' namespace_name.json

Now, that we have removed the finalizers, we need to replace this manifest via kubernetes API.

kubectl replace --raw "/api/v1/namespaces/namespace_name/finalize" -f ./namespace_name.json

This should delete the stuck namespace.