Skip to content

Instantly share code, notes, and snippets.

@ipedrazas
Created June 19, 2017 08:57
Show Gist options
  • Save ipedrazas/9c622404fb41f2343a0db85b3821275d to your computer and use it in GitHub Desktop.
Save ipedrazas/9c622404fb41f2343a0db85b3821275d to your computer and use it in GitHub Desktop.
Delete evicted pods
kubectl get pods | grep Evicted | awk '{print $1}' | xargs kubectl delete pod
@mudssrali
Copy link

mudssrali commented Apr 19, 2022

This works for me

kubectl delete pods $(kubectl get pods | grep [pod name] | grep Evicted | awk '{print $1}')

@MattyKuzyk
Copy link

Why doesn't Kubernetes clean up Evicted pods by itself? I only notice it happen sometimes.

@SunnyFenng
Copy link

SunnyFenng commented Jul 11, 2022

kubectl get pod -A | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs -n 2 kubectl delete pod

@magf
Copy link

magf commented Aug 4, 2022

i think the most simple command is
kubectl delete pods -A --field-selector=status.phase=Failed

best solution! 👍

@luizvinhas
Copy link

i think the most simple command is kubectl delete pods -A --field-selector=status.phase=Failed

works for me

@andyfcx
Copy link

andyfcx commented Dec 5, 2022

kgpa | grep Evicted | awk '{print $2}' | xargs kubectl delete pod --force

@andyfcx
Copy link

andyfcx commented Dec 6, 2022

kgpa | grep -v Running | awk '{print $2}' | xargs kubectl delete pod --force

@karatedog
Copy link

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

The original question is about to delete "Evicted" pods, which is a subset of "Failed". Unfortunately there is no state field for pods that are in the Running state.

@VictoriaYaa
Copy link

kubectl get po -A --all-namespaces -o json | jq  '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete po \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c

Thank you!

@promisepreston
Copy link

i think the most simple command is kubectl delete pods -A --field-selector=status.phase=Failed

Works for me too, for deleting failed pods in all namespaces

@albertoeks
Copy link

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

Great answer, thanks!

@recumbent
Copy link

Why doesn't Kubernetes clean up Evicted pods by itself? I only notice it happen sometimes.

My understanding is that there is a threshold for cleaning up - when the number of failed hits that threshold then clean up will happen - the challenge is that the default for that is 12500 (twelve thousand five hundred). The purpose of the threshold is to allow for review of the reasons for failure and I can see that in a large system that might almost be a reasonable number.

That threshold can be changed - I'm not sure what a sensible number would look like for a small cluster.

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