After you try to delete a namespace in a Kubernetes cluster, the namespace may be stuck in the terminating state for a long time. This topic describes how to delete a namespace in the terminating state.
Problem description
$ kubectl delete ns <namespacename>
Error from server (Conflict): Operation cannot be fulfilled on namespaces "<namespacename>": The system is ensuring all content is removed from this namespace. Upon completion, this namespace will automatically be purged by the system.
$ kubectl describe ns <namespacename>
Name: <namespacename>
Labels: <none>
Annotations: kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Namespace","metadata":{"annotations":{},"name":"<namespacename>","namespace":""}}
Status: Terminating
Cause
Residual resources exist in the namespace that you want to delete from the cluster.
Solution
To delete a namespace in the terminating state, you can delete the finalizers field in the namespace configuration.
This method can clear a namespace that is stuck in the terminating state. However, resources that belong to the namespace cannot be automatically deleted and thus left in the cluster. You must manually delete the residual resources. After the residual resources are deleted, you can perform the following steps to clear the array of the finalizers field and delete the finalizers field. Then, Kubernetes deletes the namespace in the terminating state.