Last active
January 13, 2025 17:40
-
-
Save zparnold/0e72d7d3563da2704b900e3b953a8229 to your computer and use it in GitHub Desktop.
A simply script to delete all failed pods from Kubernetes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you @rubenpetrosyan1. This worked well for me. All the other versions using
--field-selector
is not working for some reason.