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 -
- Delete all PODS in a single namespace - e.g. kubectl delete --all pods --namespace=foo
- Delete deployment in namespace which will delete PODS - e.g. kubectl delete deployment <deployment_name>
- Delete all namespaces and every object in every namespace -e.g. kubectl delete all --all --all-namespaces
- Delete everything except kubespace
- Adding sed to delete POD
- 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 -
- first all - It is going to delete all the resources such as pods, replicasets, deployments...
- 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
Learn more On Kubernetes -
- Setup kubernetes on Ubuntu
- Setup Kubernetes on CentOs
- Setup HA Kubernetes Cluster with Kubespray
- Setup HA Kubernetes with Minikube
- Setup Kubernetes Dashboard for local kubernetes cluster
- Setup Kubernetes Dashboard On GCP(Google Cloud Platform)
- How to use Persistent Volume and Persistent Volume Claims in Kubernetes
- Deploy Spring Boot Microservice on local Kubernetes cluster
- Deploy Spring Boot Microservice on Cloud Platform(GCP)
- Setting up Ingress controller NGINX along with HAproxy inside Kubernetes cluster
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- kubectl export YAML | Get YAML for deployed kubernetes resources(service, deployment, PV, PVC....)
- How to setup kubernetes jenkins pipeline on AWS?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- How to fix kubernetes pods getting recreated?
- How to delete all kubernetes PODS?
- How to use Kubernetes secrets?
- Share kubernetes secrets between namespaces?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Delete Kubernetes POD stuck in terminating state?
Posts in this Series
- Kubernetes Cheat Sheet for day to day DevOps operations?
- Delete Kubernetes POD stuck in terminating state?
- How to Delete PV(Persistent Volume) and PVC(Persistent Volume Claim) stuck in terminating state?
- Share kubernetes secrets between namespaces?
- How to use Kubernetes secrets?
- How to delete all kubernetes PODS?
- kubernetes pods getting recreated?
- Implementing Kubernetes liveness, Readiness and Startup probes with Spring Boot Microservice Application?
- kubectl export yaml OR How to generate YAML for deployed kubernetes resources
- Kubernetes Updates
- CI/CD Kubernetes | Setting up CI/CD Jenkins pipeline for kubernetes
- Kubernetes cluster setup with Jenkins
- How to use Persistent Volume and Persistent Claims | Kubernetes
- How to fix ProvisioningFailed persistentvolume controller no volume plugin matched
- Fixing – Cannot bind to requested volume: storageClasseName does not match
- Fixing – pod has unbound immediate persistentvolumeclaims or cannot bind to requested volume incompatible accessmode
- How to fix kubernetes dashboard forbidden 403 error – message services https kubernetes-dashboard is forbidden User
- How to fix Kubernetes – error execution phase preflight [preflight]
- Deploy Spring Boot microservices on kubernetes?
- How to fix – ansible_memtotal_mb minimal_master_memory_mb
- How to use kubespray – 12 Steps for Installing a Production Ready Kubernetes Cluster
- How to setup kubernetes on CentOS 8 and CentOS 7
- How to fix – How to fix - ERROR Swap running with swap on is not supported. Please disable swap
- 14 Steps to Install kubernetes on Ubuntu 20.04(bento/ubuntu-20.04), 18.04(hashicorp/bionic64)
- Kubernetes Dashboard | Kubernetes Admin GUI | Kubernetes Desktop Client
- Install Kubernetes with Minikube