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.
You may use command substitution:
Kubectl delete $(kubectl get pod -l app=foo -o name)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With