Chasing DevOps

A blog about software development, DevOps, and delivering value.

Script: Deleting all Evicted Pods in Kubernetes

Do you have a lot of evicted pods that need to be deleted in your Kubernetes cluster? Use the script below to clean them up. This script will search for evicted pods across all namespaces and delete them. You’ll find both a Powershell and bash version below.

What are evicted pods?

Kubernetes will evict pods from a node when the resources on that node (CPU, memory, etc) are under stress. These pods will remain visible in your cluster until they are manually deleted. This is by design. If you are seeing evicted pods it means one or more nodes in the cluster have been under significant stress and could be a sign of a larger underlying problem.

Powershell

kubectl get pod allnamespaces `
| Select-String Evicted `
| % {$_ -Replace \s{2,}, } `
| ConvertFrom-Csv Delimiter Header namespace, pod `
| foreach { kubectl delete pod $_.pod n $_.namespace }

Bash

kubectl get pod –all-namespaces | \
grep Evicted | \
while read line; \
do
namespace=$(echo $line | awk {print $1});
pod=$(echo $line | awk {print $2});
kubectl delete pod $pod -n $namespace;
done;

Leave a Reply

Your email address will not be published.

Jesse Barocio

Software developer, DevOps engineer, and productivity tool nut. Continuously improving. Have a question or problem you need solved? Email me!