How to delete all kubernetes PODS?


In kubernetes cluster we always creat and delete kubernetes PODS. PODS lifecycle is very short and it is very often that kubernetes creat a replica of a POD to provide maximum availability across the kubernetes cluster.

But considering the need, a developer might need to delete POD manually because of POD getting stuck in terminating status and kubernetes is not able to terminate the POD.

There are many alternate ways to delete the POD. Here I have composed some of my favorite ways to delete the POD inside the kubernetes cluster -

  1. Delete all PODS in a single namespace - e.g. kubectl delete –all pods –namespace=foo
  2. Delete deployment in namespace which will delete PODS - e.g. kubectl delete deployment <deployment_name>
  3. Delete all namespaces and every object in every namespace -e.g. kubectl delete all –all –all-namespaces
  4. Delete everything except kubespace
  5. Adding sed to delete POD
  6. Delete the pod stuck in terminating state

1. Delete all PODS in a single namespace - e.g. kubectl delete –all pods –namespace=foo

In your Kubernetes cluster, there can be more than one namespace and each POD belongs to some specific namespace, so the first approach which I would take is to delete all the POD in a single namespace.

But before deleting the POD you must know the name of the namespace in which your POD exists along with its status.

Check POD status

1kubectl describe pod pod_name --namespace foo

Delete POD

1kubectl delete --all pods --namespace=foo

2. Delete deployment in namespace which will delete PODS - e.g. kubectl delete deployment <deployment_name>

The second approach which I would take is by deleting all the deployment which belongs to POD, this approach is indirect because in a typical Kubernetes POD we do multiple deployments of the docker container.

So if you delete all the deployment in a POD then there will be no active deployment and the POD will be empty and the empty POD does not exist in a Kubernetes cluster.

Delete all deployment in a single namespace

1kubectl delete --all deployments --namespace=foo 

Delete all the deployment across all the namespaces

1kubectl delete --all deployments

Running the above command will delete all the active deployment running inside the POD which will eventually lead you to delete the desired POD inside the Kubernetes cluster.

(Note * - Always be careful about running the delete deployment command without any namespaces because it may delete all of your deployment by accident.)


3. Delete all namespaces and every object in every namespace -e.g. kubectl delete all –all –all-namespaces

The third approach for deleting POD would be to use the following delete command (But be careful and read the notes before running this command) -

1kubectl delete all --all --all-namespaces

Notes - There are two all flags in the command but each flag has its importance -

  1. first all - It is going to delete all the resources such as pods, replicasets, deployments…
  2. second all - Select all the resources of a selected kind

The above command does not delete clusterrolebindings, clusterroles, configmaps, rolebindings, roles, secrets


4. Delete everything except kubespace

The fourth way would be to use a bit of scripting and for a loop. The following command will loop over all the namespace present in the Kubernetes cluster and delete each namespace one by one.

Deleting of each namespace will eventually delete the POD running inside the namespace -

1for each in $(kubectl get ns -o jsonpath="{.items[*].metadata.name}" | grep -v kube-system);
2do
3  kubectl delete ns $each
4done

5. Adding sed to delete POD

The Fifth way would be to use the sed to delete the POD. The following command will delete all the namespaces and all the PODS running inside those namespaces.

1kubectl get pods --no-headers=true --all-namespaces |sed -r 's/(\S+)\s+(\S+).*/kubectl --namespace \1 delete pod \2/e' 

6. Delete the pod stuck in terminating state

If the previous steps solutions do not work for you then I would recommend deleting all the PODS which are stuck in the terminating state. This can happen sometimes and in such cases, you need to delete them forcefully

Here is the command -

1kubectl delete pods --all --grace-period=0 --force --namespace namespace

7. Conclusion

If you need to delete the POD many times inside the Kubernetes cluster then it is a not good practice to follow. You should fix the underlying issues which can be hiding somewhere inside the docker container. But in many cases, there can be a problem associated with the way you have set up your Kubernetes cluster and in such cases, you have to delete the POD manually.

The above-mentioned command for deleting the POD always comes in handy when you need to delete the POD so that you can work with a clean Kubernetes cluster. Hope this post helps you to troubleshoot the issue with the deletion of the POD.

You can read more on developer forum such as Stackoverflow


Other articles on Devops -

  1. Setup kubernetes on Ubuntu
  2. Setup Kubernetes on CentOs
  3. Setup HA Kubernetes Cluster with Kubespray
  4. Setup HA Kubernetes with Minikube
  5. Setup Kubernetes Dashboard for local kubernetes cluster
  6. Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
  7. How to use Persistent Volume and Persistent Volume Claims in Kubernetes
  8. Deploy Spring Boot Microservice on local Kubernetes cluster
  9. Deploy Spring Boot Microservice on Cloud Platform(GCP)
  10. Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
  11. CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
  12. kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC….)
  13. How to setup kubernetes jenkins pipeline on AWS?
  14. Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
  15. How to fix kubernetes pods getting recreated?
  16. How to delete all kubernetes PODS?
  17. How to use Kubernetes secrets?
  18. Share kubernetes secrets between namespaces?