51

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

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 below error in commandline:

~ $ 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 ?

thanks

D3vtr0n
  • 2,774
  • 3
  • 33
  • 53
pauldx
  • 833
  • 1
  • 12
  • 22

5 Answers5

75

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
  • 121,568
  • 97
  • 310
  • 388
  • 5
    that doesn't answer my question though ... 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 and put that command into .bash_profile so that I don't need to type everytime . How can I achieve this ? your answer doesn't address that . – pauldx Jan 01 '20 at 19:59
  • 1
    yes, I answered on how this usually is done when working with Kubernetes. I have updated my answer with how you delete both apps with a single command. – Jonas Jan 01 '20 at 20:14
  • 1
    thanks and it works ... I realize I have to do something like this : kubectl delete pods -l app.kubernetes.io/name=middleware because I have no names specified . So referring here if somebody has same issue – pauldx Jan 28 '20 at 17:21
  • 1
    This is the k8s style answer. – knt5784 Nov 30 '22 at 06:54
56

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
  • 11,595
  • 6
  • 65
  • 80
5

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
  • 3,665
  • 10
  • 21
  • I get error while running this alias in commandline or through .bash_profile : kdpgroup error: resource(s) were provided, but no name, label selector, or --all flag specified . Also while set into .bash_profile and ended up with below error : . ./.bash_profile -bash: ./.bash_profile: line 33: syntax error near unexpected token `(' -bash: ./.bash_profile: line 33: `kdpgroup() {' – pauldx Jan 01 '20 at 19:53
  • 1
    @pauldx, I'm sorry, I forgot to include `xargs` on the alias command. I've updated the command, could try again and see if this solves your problem. – Mr.KoopaKiller Jan 02 '20 at 08:53
  • 1
    apologize delay reply . Yes this perfectly works . thanks – pauldx Jan 28 '20 at 17:29
3

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
  • 15,216
  • 3
  • 86
  • 85
1

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

Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121