1. Introduction
Deleting pods individually is okay when deleting one or a few pods in one namespace. But when we have to remove multiple pods across various namespaces, deleting each pod discretely can be a bit demanding. Thankfully, there’s a way to delete all pods in all Kubernetes namespaces at once.
In this tutorial, we discuss how to delete all pods in all namespaces of a Kubernetes cluster. We also talk about how to delete all pods in one or a few namespaces and how to delete all resources in all, one, or a few namespaces.
2. Delete All Pods in All Namespaces
We can delete all pods in all namespaces by adding –all and –all-namespaces to the usual kubectl delete pods command:
$ kubectl delete pods --all --all-namespaces
Replacing –all-namespaces with -A makes the syntax shorter:
$ kubectl delete pods --all -A
Going by the commands above, rather than specify the name of the pod to delete, we’ll use the –all flag to indicate that we want to delete all pods. This will help us delete multiple pods with a single command.
Also, instead of deleting the pods one namespace at a time, we’ll use the –all-namespaces or -A flag to delete pods in all namespaces.
Let’s try running the command in our terminal:
$ kubectl delete pods --all -A
pod "test-pod" deleted
pod "coredns-787d4945fb-w6j2h" deleted
pod "etcd-minikube" deleted
pod "kube-apiserver-minikube" deleted
pod "kube-controller-manager-minikube" deleted
pod "kube-proxy-7vm2m" deleted
pod "kube-scheduler-minikube" deleted
pod "storage-provisioner" deleted
Looking at the output above, the command also deletes pods in the kube-system namespace (and potentially other Kubernetes system namespaces).
But then, those kube-system pods might get replicated almost immediately:
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-787d4945fb-jcg9m 1/1 Running 0 19s
etcd-minikube 1/1 Running 4 (2d8h ago) 19s
kube-apiserver-minikube 1/1 Running 4 (11m ago) 19s
kube-controller-manager-minikube 1/1 Running 4 (2d8h ago) 19s
kube-proxy-f7zgm 1/1 Running 0 17s
kube-scheduler-minikube 1/1 Running 4 (2d8h ago) 18s
The cluster should work fine after the kube-system pods are replicated. Still, it’s better to exclude the kube-system namespace while deleting all the pods in all namespaces since deleting all of the pods in the kube-system namespace can make the cluster unstable.
Replication is not peculiar to pods in the kube-system. Generally, Kubernetes clusters replicate pods created from deployments in any namespace. But, if we do not want our cluster to replicate them, we’ll have to delete the deployments:
$ kubectl delete deploy --all -A
deployment.apps "app-deployment" deleted
deployment.apps "coredns" deleted
The command above deletes all deployments in all namespaces.
2.1. Excluding Kubernetes System Namespaces With a for Loop
Using a for loop, we can exclude Kubernetes system namespaces when deleting all pods in all namespaces:
$ for ns in $(kubectl get namespaces -o name | grep -v kube- | cut -c 11-);
do
kubectl delete pods --all -n $ns;
done
pod "liveness-exec" deleted
The command above gets all the namespaces in the cluster and redirects the output to grep.
grep inverts the output (the namespaces), excluding any Kubernetes system namespace. The grep command worked for us because, as recommended by Kubernetes, we did not create any of our namespaces with the kube- prefix.
The grep -v kube- command excludes Kubernetes system namespaces from the delete directive. Then, its output is piped to cut.
cut removes the first 10 characters of the output, which is the string, “namespace/”, from grep‘s output. This leaves just the name of the non-Kubernetes namespaces.
Next, the command does a for loop through each namespace $ns while running kubectl delete pods –all -n $ns. This deletes all the pods in all the namespaces except for the Kubernetes system namespaces.
Using the same for loop, we can delete deployments in all namespaces, excluding the Kubernetes system namespaces:
$ for ns in $(kubectl get namespaces -o name | grep -v kube- | cut -c 11-);
do
kubectl delete deploy --all -n $ns;
done
deployment.apps "app-deployment" deleted
2.2. Excluding Kubernetes System Namespaces With –field-selector
We can exclude Kubernetes system namespaces from the delete command using the –field-selector flag:
$ kubectl delete pods -A --field-selector metadata.namespace!=kube-system,metadata.namespace!=kube-public,metadata.namespace!=kube-node-lease
pod "app-deployment-5dc467b756-pcfwk" deleted
pod "app-deployment-5dc467b756-qg9l2" deleted
pod "app-deployment-5dc467b756-nhc2k" deleted
pod "app-deployment-5dc467b756-r6jj2" deleted
The –field-selector flag filters the queries of the kubectl delete pods command. In this case, we used the not equal sign, !=. Hence, the command excluded the kube-system, kube-public, and kube-node-lease namespaces from its queries.
The same syntax works when deleting deployments and other resources:
$ kubectl delete deploy -A --field-selector metadata.namespace!=kube-system,metadata.namespace!=kube-public,metadata.namespace!=kube-node-lease
deployment.apps "app-deployment" deleted
deployment.apps "app-deployment" deleted
3. Delete All Pods in One Namespace
To avoid deleting pods in Kubernetes system namespaces, we can delete all pods one namespace at a time:
$ kubectl delete pods --all -n [namespace]
This way, we can ensure our cluster remains stable while we adjust the pods as needed.
Let’s delete all pods in the baeldung namespace:
$ kubectl delete pods --all -n baeldung
pod "app-deployment-5dc467b756-h4m9d" deleted
pod "app-deployment-5dc467b756-p46jq" deleted
4. Delete All Pods in Multiple Namespaces
With Bash brace expansion and eval, we can delete all pods in multiple namespaces:
$ eval kubectl\ delete\ pods\ --all\ --namespace={namespace1,namespace2,namespace3}\;
Let’s delete all pods in the default and baeldung namespaces:
$ eval kubectl\ delete\ pods\ --all\ --namespace={default,baeldung}\;
pod "app-deployment-5dc467b756-4bk98" deleted
pod "app-deployment-5dc467b756-mlnq6" deleted
pod "liveness-exec" deleted
pod "app-deployment-5dc467b756-scchm" deleted
pod "app-deployment-5dc467b756-z2h8v" deleted
pod "liveness-exec" deleted
5. Delete All Resources in All Namespaces
We can delete all the resources – not just pods – in all namespaces:
$ kubectl delete all --all --all-namespaces
We can use the shorter syntax here, too:
$ kubectl delete all --all -A
Let’s delete all resources in our Kubernetes cluster:
$ kubectl delete all --all -A
pod "app-deployment-5dc467b756-kmr6k" deleted
...truncated...
pod "kube-apiserver-minikube" deleted
...truncated...
service "kube-dns" deleted
daemonset.apps "kube-proxy" deleted
deployment.apps "app-deployment" deleted
...truncated...
As seen in the output, the command deletes pods, services, daemonsets, and deployments across all namespaces, including the kube-system namespace. But, there’s a potential problem: Some of the resources deleted in the kube-system namespace were not replicated. Of course, this means the cluster may not function correctly.
So, unless we plan to destroy our cluster, we are unlikely to use this command.
6. Delete All Resources in One Namespace
Instead of deleting all resources in all namespaces, we can take a more cautious approach by deleting all resources in one namespace at a time. By doing this, there’s little or no chance of unintentionally deleting resources in the kube-system namespace.
Let’s delete all resources in the baeldung namespace:
$ kubectl delete all --all -n baeldung
pod "app-deployment-5dc467b756-8vwgq" deleted
pod "app-deployment-5dc467b756-jtw7c" deleted
pod "liveness-exec" deleted
deployment.apps "app-deployment" deleted
replicaset.apps "app-deployment-5dc467b756" deleted
7. Delete All Resources in Multiple Namespaces
Similar to what we did before, we can delete all resources in multiple namespaces using eval and Bash brace expansion:
$ eval kubectl\ delete\ all\ --all\ --namespace={namespace1,namespace2}\;
Let’s try that out on our default and baeldung namespaces:
$ eval kubectl\ delete\ all\ --all\ --namespace={default,baeldung}\;
pod "app-deployment-5dc467b756-r9x6r" deleted
pod "app-deployment-5dc467b756-zb59m" deleted
pod "liveness-exec" deleted
service "kubernetes" deleted
deployment.apps "app-deployment" deleted
replicaset.apps "app-deployment-5dc467b756" deleted
pod "app-deployment-5dc467b756-5grpz" deleted
pod "app-deployment-5dc467b756-wjbgd" deleted
pod "liveness-exec" deleted
deployment.apps "app-deployment" deleted
replicaset.apps "app-deployment-5dc467b756" deleted
8. Conclusion
In this article, we saw how to delete all pods in one namespace, multiple namespaces, and all namespaces of a Kubernetes cluster. Similarly, we discussed deleting all resources in one namespace, multiple namespaces, and all namespaces.
To delete all pods in all namespaces of our cluster, we simply used the –all flag in place of pod names. Then, instead of specifying a single namespace, we used the –all-namespaces flag. The same syntax works when deleting other Kubernetes resources.