Kubernetes — Delete All Pods Evicted State


In K8s sometimes when we run pods, due to lack of resources like memory or CPU or some application error pods get evicted. In this case Kubernetes will try restart these evicted pods but , if there are still no resources left or the application has any errors, the pod will still be in evicted state.

Should we care about this?

Definitely yes, when we have many evicted pod in the cluster it can lead to a serious network load as each pod, although evicted, is connected to the network and is blocking the IP address which can lead to exhaustion. of course, if we have a fixed pool of addresses for our cluster.

How Delete Evicted Pods ?

The easiest way to do this is to use the kubectl delete pod command to delete pod in Kuberenetes. This is a bit limiting because with this command we have to give the name of a particular pod to delete it.

Things get complicated when we have 200 pods that are in evicted state. Running kubectl delete pod 200 times was terrible.

To quickly remove a large number of pods in evicted status, execute the following command.

ozyrys@ozyrys-Lenovo-ideapad-700-15ISK:~$ kubectl get pod -n develop | grep Evicted | awk '{print $1}' | xargs kubectl delete pod -n develop

⚠️ If we need to remove the pod in a state other than Evicted , just change the word Evicted to CrashLoopBackOff and run command.

Source: https://giphy.com

 by the author.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


DevOps Consultant. I’m strongly focused on automation, security, and reliability.