[Kubernetes (K8S) FAQs] Delete the terminating resources in Kubernetes (K8S)

In some cases, a resource cannot be deleted in Kubernetes (K8S), and it will remain stuck in terminating state.

The steps to solve this problem are:

For example, there are some Namespace resources stucked in terminating state.

Remember to replace nginx-ingress with your prefer namespace name.

1
2
3
4
5
6
7
$ K8S_NAMESPACE=nginx-ingress

# Confirm and delete manually
$ kubectl get ns ${K8S_NAMESPACE} -o json > ${K8S_NAMESPACE}.json && vi ${K8S_NAMESPACE}.json && kubectl replace --raw "/api/v1/namespaces/${K8S_NAMESPACE}/finalize" -f ${K8S_NAMESPACE}.json

# Or Auto delete
$ kubectl patch ns example -p '{"metadata":{"finalizers":[]}}' --type=merge

Delete multiple Namespace resources.

Remember to replace nginx-ingress and cloudolife with your prefer namespace name.

1
2
3
4
5
6
7
8
$ IMAGE_VERSIONS="nginx-ingress cloudolife"

$ for K8S_NAMESPACE in ${K8S_NAMESPACES}; \
do \
kubectl patch ns ${K8S_NAMESPACE} -p '{"metadata":{"finalizers":[]}}' --type=merge

kubectl get ns ${K8S_NAMESPACE} -o json > ${K8S_NAMESPACE}.json && sed -i '/"kubernetes"/'d ${K8S_NAMESPACE}.json && kubectl replace --raw "/api/v1/namespaces/${K8S_NAMESPACE}/finalize" -f ${K8S_NAMESPACE}.json; \
done

The above command will delete the all elements(such as kubernetes) in finalizer array, leaving only an empty array [] with the namespace resource.

References

[1] K8S如何更新Terminating状态的命名空间?_云容器引擎 CCE_常见问题_集群类_集群运行_华为云 - https://support.huaweicloud.com/cce_faq/cce_faq_00277.html

[2] k8s 删除ns 卡住的解决办法 - ElNinoT - 博客园 - https://www.cnblogs.com/elnino/p/11104084.html

[3] k8s 删除ns 卡住的解决办法 - osc_m8nx82a1的个人空间 - OSCHINA - 中文开源技术交流社区 - https://my.oschina.net/u/4385577/blog/3483266

[4] k8s所有的NS删除的时候都进入Terminating状态 | LiarLee’s Notebook - https://liarlee.site/2019/10/09/Linux/Linux_k8s-namespace-delete-terminating/

[5] K8s CRD资源一直Terminating删除不掉_o0haidee0o的博客-CSDN博客_k8s 删除crd - https://blog.csdn.net/o0haidee0o/article/details/116745171