Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cary205/500793572d9cb28ecc9c9b9027b7a232 to your computer and use it in GitHub Desktop.
Save cary205/500793572d9cb28ecc9c9b9027b7a232 to your computer and use it in GitHub Desktop.
Delete evicted pods from all namespaces (also ImagePullBackOff and ErrImagePull)
#!/bin/sh
# based on https://gist.github.com/ipedrazas/9c622404fb41f2343a0db85b3821275d
# delete all evicted pods from all namespaces
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
# delete all containers in ImagePullBackOff state from all namespaces
kubectl get pods --all-namespaces | grep 'ImagePullBackOff' | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
# delete all containers in ImagePullBackOff or ErrImagePull or Evicted state from all namespaces
kubectl get pods --all-namespaces | grep -E 'ImagePullBackOff|ErrImagePull|Evicted' | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
@cary205
Copy link
Author

cary205 commented Feb 25, 2022

To delete evicted pods from all the namespaces, I use:

kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs -n 2 -d '\n' bash -c 'kubectl delete pod $0 $1'

This will split the parameters correctly for xargs when using the namespace option. Without those options, xargs was joining the output sending something like: kubectl delete pod pod-a --namespace 1 pode-b --namespace 2.

@cary205
Copy link
Author

cary205 commented Mar 18, 2022

in Rancher

kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs -n 2 bash -c 'kubectl delete pod $0 $1'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment