Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a convinient way to pipe multiple kubectl commands to each other?

I am looking for a way to do something like that

kubectl get pods -l app=foo | kubectl delete

I think this would be a great way to deal with multiple ressources at once, but I cannot find a reasonably easy way to do this.

like image 727
Max Avatar asked Dec 30 '25 13:12

Max


2 Answers

You may use command substitution:

Kubectl delete $(kubectl get pod -l app=foo -o name)
like image 124
P.... Avatar answered Jan 01 '26 11:01

P....


kubectl get -o name will write out resource names in kind/name format, one to a line. You can use this in combination with tools like xargs(1) to run pipelines like you suggest.

kubectl get job -l app=foo -o name | xargs kubectl delete

# help we're using the long-format label names and I don't remember
# what goes after `kubectl get -l`
kubectl get job -o name | grep foo | xargs kubectl delete

For the very specific command you show, I've often found it easier to use kubectl rollout restart to trigger a Deployment's redeployment sequence without actually making any changes. This will delete all of the pods managed by the Deployment, but only after creating new pods first; so you get the effect of restarting misbehaving Pods but without actually taking the whole application down.

kubectl rollout restart deployment/foo
like image 23
David Maze Avatar answered Jan 01 '26 10:01

David Maze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!