Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save svx/a2310feb53fb71ad232bd7f66fada5bc to your computer and use it in GitHub Desktop.
Save svx/a2310feb53fb71ad232bd7f66fada5bc 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
@svx
Copy link
Author

svx commented Aug 15, 2018

kubectl get pods -a --all-namespaces | grep -E 'ImagePullBackOff|ErrImagePull|Evicted|Error|Completed'

@ilanni2460
Copy link

kubectl get po --all-namespaces | awk '{if ($4 != "Evicted") system ("kubectl -n " $1 " delete pods " $2 " --grace-period=0 " " --force ")}'

@snafuprinzip
Copy link

kubectl get po --all-namespaces | awk '{if ($4 != "Evicted") system ("kubectl -n " $1 " delete pods " $2 " --grace-period=0 " " --force ")}'

($4 != "Evicted") will delete all pods that have not been Evicted, which is the direct opposite of what you want usually.

@moshucong
Copy link

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

@GMartinez-Sisti
Copy link

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.

@venkatesh-phenompeople
Copy link

kubectl get po --all-namespaces | awk '{if ($4 != "Evicted") system ("kubectl -n " $1 " delete pods " $2 " --grace-period=0 " " --force ")}'

hey this is very wrong

@chengzi-xs
Copy link

do not use kubectl get po --all-namespaces | awk '{if ($4 != "Evicted") system ("kubectl -n " $1 " delete pods " $2 " --grace-period=0 " " --force ")}' this will delete all evited satus pods

@alperen-selcuk
Copy link

kubectl get pods --all-namespaces | grep Evicted | awk '{print "kubectl delete pods -n " $1 " " $2 " --grace-period=0 --force"}' | sh

@SpikePy
Copy link

SpikePy commented Oct 13, 2021

Show all Failed/Evicted Pods

kubectl get pod --all-namespaces --field-selector=status.phase==Failed

Delete all Failed/Evicted Pods

kubectl delete pod --all-namespaces --field-selector=status.phase==Failed

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