When you deploy an application in a Kubernetes cluster, you usually set some labels on the Pods e.g. app=my-app
.
These labels can be used as a wildcard for selecting a group of Pods for deletion.
This short note shows how to perform a wildcard deletion of Pods in Kubernetes.
Cool Tip: Get Pods on specific Node in Kubernetes! Read more →
Wildcard Pods Deletion using Kubectl
List all Pods in the current Namespace and show their labels:
$ kubectl get pods --show-labels
Get the Pods with a particular label, e.g. app=my-app
:
$ kubectl get pods -l app=my-app
Delete all the Pods with the label app=my-app
:
$ kubectl delete pods -l app=my-app
Alternatively the wildcard deletion of the Pods in the current namespace can be implemented as follows:
$ kubectl get pods --no-headers=true|awk '/app/{print $1}'|xargs kubectl delete pod
The command above deletes all the Pods that contain app
in their names and gives a response as follows:
pod "my-app-prd-h33ee" deleted pod "my-app-prd-jso93" deleted pod "my-app-prd-bdm3s" deleted
Cool Tip: List & Change Namespaces in Kubernetes! Read more →