-
Star
(104)
You must be signed in to star a gist -
Fork
(18)
You must be signed in to fork a gist
-
-
Save zparnold/0e72d7d3563da2704b900e3b953a8229 to your computer and use it in GitHub Desktop.
| kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod |
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.
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! 🎉
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.).
@TobiasWenzel thanks for drawing this to my attention :)
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=EvictedHowever, for me it didn't work out of the box, the
=after--field-selectorhas to be removed to make it work andEvictedhas to be replaced withFailed("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-namespacesBear 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
Great!
Very helpful!
thanks for you all guys! very usefull
kubectl delete pods -A --field-selector=status.phase=Failed
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
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.
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.
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.
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
Thank you @rubenpetrosyan1. This worked well for me. All the other versions using --field-selector is not working for some reason.
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
@rubenpetrosyan1 good, it's works
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
kubectl delete pods --field-selector status.phase=Failed --all-namespaces
kubectl delete pods --field-selector status.phase=Error --all-namespaces
kubectl delete pods --field-selector status.phase=Succeeded --all-namespaces
#ALL
kubectl delete pods --field-selector status.phase!=Running --all-namespaces
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
kubectl delete pods --field-selector status.phase=Failed --all-namespaces kubectl delete pods --field-selector status.phase=Error --all-namespaces kubectl delete pods --field-selector status.phase=Succeeded --all-namespaces #ALL kubectl delete pods --field-selector status.phase!=Running --all-namespaces
thank you so much - this is the cleanest by far
The simplest solution possible would be like
NAMESPACE="test"
kubectl delete pods -n $NAMESPACE --field-selector=status.phase!=Running
That command is needlessly complicated. You just use:
kubectl delete pods --field-selector=status.phase=EvictedBut for really complicated stuff involving kubectl and powershell, you have to use something like
([string](kubectl get pods -o json)|convertfrom-json).itemsto get started.