Skip to content

Instantly share code, notes, and snippets.

@zparnold
Last active April 25, 2024 21:56
Show Gist options
  • Star 91 You must be signed in to star a gist
  • Fork 19 You must be signed in to fork a gist
  • Save zparnold/0e72d7d3563da2704b900e3b953a8229 to your computer and use it in GitHub Desktop.
Save zparnold/0e72d7d3563da2704b900e3b953a8229 to your computer and use it in GitHub Desktop.
A simply script to delete all failed pods from Kubernetes
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
@psxvoid
Copy link

psxvoid commented Aug 6, 2018

An extended @tombh answer for working with all namespaces:
https://gist.github.com/psxvoid/71492191b7cb06260036c90ab30cc9a0

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

@stieler-it
Copy link

@psxvoid this does not seem to work if multiple namespaces have evicted pods. In this case, it works only for the first namespace, so you would have to call it multiple times.

@diegodurante
Copy link

Thanks for all information.

I prefer always to specify the namespace so this is the command that I use to delete old failed/evicted pods:

kubectl --namespace=production get pods -a | grep Evicted | awk '{print $1}' | xargs kubectl --namespace=production delete pod -o name

Note the little option -a that shows all pods, without that was not working for me. Also I changed how I print values after the grep.

Hope it helps!

@spotlesscoder
Copy link

Please add a license :)

@VinayVanama
Copy link

kubectl get pods | grep Evicted | awk '{print $1}' | xargs kubectl delete pod

Better way for this is including namespace too
kubectl get pods -n name-spacename | grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n name-spacename

@ilanni2460
Copy link

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

@ORESoftware
Copy link

If you delete a pod it just restarts right? don't we want --grace-period=0 and/or --force too?

@zparnold
Copy link
Author

Yep, but that's only if it's owned by something that would reschedule it elsewhere. Such is the case with a deployment or a replicaset. For example if a pod spawned by a job fails...depending on the policy you could end up with a lot of extra garbage if the job was tried over and over again. Hence this script

@zparnold
Copy link
Author

To everyone here, this was useful to us earlier before we got better at kubectl (plus it gave us a file based backup if we needed to know pod ID's later for post-mortems,) but there are many ways to use kubectl to do this without a script...a lot of the people on this commenting thread have make great suggestions...please use theirs instead!

@mcat56
Copy link

mcat56 commented Aug 13, 2020

What is the equivalent of kubectl get pods | grep Evicted | awk '{print $1}' | xargs kubectl delete pod in Poweshell?

@willemm
Copy link

willemm commented Aug 14, 2020

That command is needlessly complicated. You just use:

kubectl delete pods --field-selector=status.phase=Evicted

But for really complicated stuff involving kubectl and powershell, you have to use something like ([string](kubectl get pods -o json)|convertfrom-json).items to get started.

@besteban1989
Copy link

An extended @tombh answer for working with all namespaces:
https://gist.github.com/psxvoid/71492191b7cb06260036c90ab30cc9a0

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

Awesome, thanks.

@zparnold
Copy link
Author

zparnold commented Sep 2, 2020

Hi all, I've updated this to reflect a much easier way to do this. I wrote this a long time ago before I knew bash, kubectl and Kubernetes all that well. Thanks for the awesome suggestions above you people rock! 🎉

@TobiasWenzel
Copy link

The solution provided by @willemm is the best, as it doesn't require other tools and works on every platform, so also under Windows.

That command is needlessly complicated. You just use:

kubectl delete pods --field-selector=status.phase=Evicted

However, for me it didn't work out of the box, the = after --field-selector has to be removed to make it work and Evicted has to be replaced with Failed ("Evicted" is the reason, "Failed" is the phase). Also you have to provide a namespace.
So I ended up with:

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

Bear in mind, however, that this not only deletes evicted pods, but also pods that have failed due to different reasons ("ContainerCannotRun", "Error", "ContainerCreating", etc.).

@zparnold
Copy link
Author

@TobiasWenzel thanks for drawing this to my attention :)

@woodgear
Copy link

The solution provided by @willemm is the best, as it doesn't require other tools and works on every platform, so also under Windows.

That command is needlessly complicated. You just use:
kubectl delete pods --field-selector=status.phase=Evicted

However, for me it didn't work out of the box, the = after --field-selector has to be removed to make it work and Evicted has to be replaced with Failed ("Evicted" is the reason, "Failed" is the phase). Also you have to provide a namespace.
So I ended up with:

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

Bear in mind, however, that this not only deletes evicted pods, but also pods that have failed due to different reasons ("ContainerCannotRun", "Error", "ContainerCreating", etc.).

make this golf even better,use -A instead of --all-namespaces.
hence kubectl delete pods --field-selector status.phase=Failed -A

@wangzhenzhe
Copy link

Great!
Very helpful!

@smilelikeshit
Copy link

thanks for you all guys! very usefull

@CarTerr
Copy link

CarTerr commented Jan 13, 2022

kubectl delete pods -A --field-selector=status.phase=Failed

@renich
Copy link

renich commented Jun 7, 2022

Or, if you want to delete all pods that are not explicitly running (completed, for example):

kubectl delete -A --field-selector 'status.phase!=Running' pods

@juniorz
Copy link

juniorz commented Jun 20, 2022

kubectl delete -A --field-selector 'status.phase!=Running' pods

Note this will also delete pods in PodInitializing, ContainerCreating and Pending status - which might not be desired.

@fabiocaldeira
Copy link

This command was just perfect to delete my evicted pods: kubectl delete pods --field-selector status.phase=Failed -A

Thank you guys for this thread! It was really helpful.

@yuzp1996
Copy link

yuzp1996 commented Jul 28, 2022

First, execute kubectl get po -A |grep Evicted |awk '{print "kubectl delete po " $2 " -n " $1 }' to check the command.

After you confirm the command is what you want then execute kubectl get po -A |grep Evicted |awk '{print "kubectl delete po " $2 " -n " $1 }' |bash to run the command.

@rubenpetrosyan1
Copy link

rubenpetrosyan1 commented Aug 26, 2022

for me, all the above was not working with the newer (v1.23+) version of Kubernetes as with kubectl get pods --all-namespaces it shows some other statuses as well, like "OutOfcpu", "OOMKilled", "ContainerStatusUnknown", etc. So I added all those statuses to @yuzp1996 command:

kubectl get pods --all-namespaces | grep -E OutOfcpu\|Evicted\|Completed\|OOMKilled\|Error\|ContainerStatusUnknown | awk '{print "kubectl delete po " $2 " -n " $1 }' | bash

@cristianuibar
Copy link

Thank you @rubenpetrosyan1. This worked well for me. All the other versions using --field-selector is not working for some reason.

@abdulnazurudeen
Copy link

My case I have to delete few specifically those have different name of pod, I will used below command to delete those.
If you know the name list to delete try like below worked for me.
kubectl delete pods podname1 anotherpod2 etcpod

@Nurlan199206
Copy link

@rubenpetrosyan1 good, it's works

@mayconritzmann
Copy link

mayconritzmann commented Mar 29, 2023

This simple loop could help:

#!/bin/bash

for ns in $(kubectl get po -A --no-headers | grep -i crash | awk {'print $1'}); do
  delpods=$(kubectl get pods -n $ns |
    grep -i 'CrashLoopBackOff' |
    awk '{print $1 }')    
  for i in ${delpods[@]}; do
    kubectl delete pod $i --force=true --wait=false \
      --grace-period=0 -n $ns 
  done
done

@BioQwer
Copy link

BioQwer commented Jun 28, 2023

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

#ALL
kubectl delete pods --field-selector status.phase!=Running --all-namespaces

@mertyakan
Copy link

kubectl get po -A -o wide| grep -vE "Compl|Runn"|awk {'print $1,$2'}|grep -v NAMESPACE| sed "s,^,kubectl delete pod --force -n ,g" |bash

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