K
Q

Kubernetes POD delete with Pattern Match or Wildcard

December 24, 2019

When I am using below it deletes the running POD after matching the pattern from the command line:

kubectl get pods -n bi-dev --no-headers=true | awk '/group-react/{print $1}' | xargs kubectl delete -n bi-dev pod

However when I am using this command as an alias in .bash_profile it doesn't execute. This is how I defined it:

 alias kdpgroup="kubectl get pods -n bi-dev --no-headers=true | awk '/group-react/{print $1}'|  kubectl delete -n bi-dev pod"

When execute this as below I get an error in the command line:

~ $ kdpgroup
error: resource(s) were provided, but no name, label selector, or --all flag specified

When I define this in .bash_profile I get this:

~ $ . ./.bash_profile
-bash: alias: }| xargs  kubectl delete -n bi-dev pod: not found
~ $

Am I missing something to delete POD using Pattern Match or with Wilcard?

-- pauldx
kubernetes
kubectl

5 Answers

December 27, 2019

Am I missing something to delete POD using Pattern Match or with Wilcard?

When using Kubernetes it is more common to use labels and selectors. E.g. if you deployed an application, you usually set a label on the pods e.g.

app=my-app
and you can then get the pods with e.g.
kubectl get pods -l app=my-app
.

Using this aproach, it is easier to delete the pods you are interested in, with e.g.

kubectl delete pods -l app=my-app

or with namespaces

kubectl delete pods -l app=my-app -n default

See more on Kubernetes Labels and Selectors

Set-based selector

I have some pod's running in the name of "superset-react" and "superset-graphql" and I want to search my wildcard superset and delete both of them in one command

I suggest that those pods has labels

app=something-react
and
app=something-graphql
. If you want to classify those apps, e.g. if your "superset" varies, you could add a label
app-type=react
and
app-type=graphql
to all those type of apps.

Then you can delete pods for both app types with this command:

kubectl delete pods -l 'app-type in (react, graphql)'
-- Jonas
Source: StackOverflow

May 14, 2021

As the question asks, this is about using a wild card. Let me give examples on using wild cards to delete pods.

Delete Pods which contain the word "application"

Replace

<namespace>
with the namespace you want to delete pods from.

kubectl get pods -n <namespace> --no-headers=true | awk '/application/{print $1}'| xargs  kubectl delete -n <namespace> pod

This will give a response like the following. It will print out the deleted pods.

pod "sre-application-7fb4f5bff9-8crgx" deleted
pod "sre-application-7fb4f5bff9-ftzfd" deleted
pod "sre-application-7fb4f5bff9-rrkt2" deleted

Delete Pods which contain "application" or "service"

Replace

<namespace>
with the namespace you want to delete pods from.

kubectl get pods -n <namespace> --no-headers=true | awk '/application|service/{print $1}'| xargs  kubectl delete -n <namespace> pod

This will give a response like the following. It will print out the deleted pods.

pod "sre-application-7fb4f5bff9-8crgx" deleted
pod "sre-application-7fb4f5bff9-ftzfd" deleted
pod "sre-service-7fb4f5bff9-rrkt2" deleted
-- Keet Sugathadasa
Source: StackOverflow

December 27, 2019

You just need to escape the '$1' variable in the awk command:

alias kdpgroup="kubectl get pods -n bi-dev --no-headers=true | awk '/group-react/{print \$1}'| xargs kubectl delete -n bi-dev pod"

I know that escape is boring, and if you want to avoid it you can use as a function in you .bash_profile:

kdpgroup() {
	kubectl get pods -n default --no-headers=true | awk '{print $1}' | xargs kubectl delete pod -n default
}
-- Mr.KoopaKiller
Source: StackOverflow

September 18, 2022

using grep you can filter the keyword like this and delete matching pod name like this

kubectl get pods   --no-headers=true | awk '{print $1}' | grep keyword |  xargs kubectl delete pod

Mermoid diagram representation of the above command :

enter image description here

-- Ram Ghadiyaram
Source: StackOverflow

June 15, 2022

A robust way with variables, based on @keetSugathadasa answer:

ns="optional-namespace"
regex="pattern"

kubectl get pods ${ns:+ -n $ns} --no-headers | awk /${regex}/'{print $1}' \ 
| xargs kubectl delete ${ns:+ -n $ns} pod
-- Noam Manos
Source: StackOverflow