Quick Tip – Kubernetes – Delete all evicted pods across all namespaces

I’m currently troubleshooting an issue with my Kubernetes clusters where pods keep getting evicted, and this is happening across namespaces as well.

The issue now that I am faced with, is being able to keep ontop of the issues. When I run:

kubectl get pods -A | grep Evicted

I’m presented with 100’s of returned results.

kubectl get pods -A grep Evicted

So to quickly clean this up, I can run the following command:

kubectl get pods -A | grep Evicted | awk '{print $1,$2,$4}' | xargs kubectl delete pod $2 -n $1

Breaking down the command:

  • Get all pods across all namespaces
  • Filter by term “Evicted”
  • Manipulate the output by selecting the data in field 1, 2 and 4
  • Use xargs to read from the standard output to place the data from the previous pipe into the “kubectl” command.

This command will cycle and remove everything you need. You can use this command line for other status of pods if needed.

kubectl get pods -A grep Evicted awk xargs kubectl delete pod

Now I need to get back to troubleshooting my cluster issues.


Dean Lewis

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.