Bash Script: Clean Up Evicted Pods

Evicted pods clutter your output and confuse your tools. Here is a safe one-liner to delete them all.

J
Jesus Paz
1 min read

If you use Spot Instances or have tight resource limits, you’ve seen them. Rows and rows of pods with status Evicted.

They don’t consume resources, but they are noise. They make kubectl get pods hard to read and can slow down some dashboards.

The Cleanup Command

Here is the command to delete only the pods that are Failed or Evicted, across all namespaces:

Terminal window
kubectl get pods -A | grep Evicted | awk '{print $1, $2}' | xargs -n2 bash -c 'kubectl delete pod $2 -n $1'

How it works

  1. kubectl get pods -A: List everything.
  2. grep Evicted: Filter for the noise.
  3. awk '{print $1, $2}': Extract the Namespace ($1) and Pod Name ($2).
  4. xargs: Pass them to the delete command.

Run this once a week, or add it to your CI/CD cleanup job. Keep your cluster tidy.

👨‍💻

Jesus Paz

Founder & CEO

Join 1,000+ FinOps and platform leaders

Get Kubernetes and ECS cost tactics delivered weekly.