Skip to content

Instantly share code, notes, and snippets.

@ipedrazas
Created June 19, 2017 08:57
Show Gist options
  • Save ipedrazas/9c622404fb41f2343a0db85b3821275d to your computer and use it in GitHub Desktop.
Save ipedrazas/9c622404fb41f2343a0db85b3821275d to your computer and use it in GitHub Desktop.
Delete evicted pods
kubectl get pods | grep Evicted | awk '{print $1}' | xargs kubectl delete pod
@abdennour
Copy link

It happen with me when I installed Kind Cluster alongside with docker-for-desktop cluster. This is how I resolve it

kubectl get po --all-namespaces --field-selector 'status.phase==Failed' -o json | kubectl delete -f -

kubectl get po --all-namespaces --field-selector 'status.phase==Evicted' -o json | kubectl delete -f -

kubectl get po --all-namespaces --field-selector 'status.phase==Pending' -o json | kubectl delete -f -

@autonomia-wiki
Copy link

For me, kubectl delete pod needs the -n cattle-system to work!
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2}' | xargs kubectl -n cattle-system delete pod

@zironycho
Copy link

kubectl get po -A | grep Evicted | awk '{system ("kubectl -n " $1 " delete po " $2)}'

@vdksystem
Copy link

why not kubectl delete pod --all-namespaces --field-selector 'status.phase==Evicted'?

@phspagiari
Copy link

@vdksystem the Evictedis under reason path and field-selector by status.reason isn't allowed. We can do this: kubectl delete pod --all-namespaces --field-selector 'status.phase=Failed'

@vdksystem
Copy link

Sorry, the question is why not kubectl delete [selector]
Looks more elegant than kubectl get pods [selector] -o json | kubectl delete -f -

@sunild
Copy link

sunild commented Jan 5, 2020

I like the command from @bronweg above using the field selectors option.

Except maybe to be safer, you can explicitly look for "Evicted" pods instead of ones who status is not "Running". I also recommend doing a kubectl get pods w/the same options to see what you're going to delete before actually deleting them

kubectl get pods --field-selector=status.phase=Evicted
kubectl delete pods --field-selector=status.phase=Evicted

@rschoultz
Copy link

While the status.phase can be running, I did not find any "Evicted" ones even though they were actually that. I had better luck using a lookup for "Failed" when it is "Evicted", i.e. :

kubectl get pods --field-selector=status.phase=Failed
kubectl delete pods --field-selector=status.phase=Failed

@phspagiari
Copy link

@rschoultz exactly! This is what I mean with my comment above. Evicted is under status.reason and this one isn't available to use as field-selector, e.g.:

#: kubectl get pods --field-selector=status.reason=Evicted
Error from server (BadRequest): Unable to find "/v1, Resource=pods" that match label selector "", field selector "status.reason=Evicted": field label not supported: status.reason
status:
  message: ""
  phase: Failed
  reason: Evicted
  startTime: ""

@zffocussss
Copy link

is there a way to remove old evicted pods?

@FL3SH
Copy link

FL3SH commented Jan 14, 2020

kubectl delete pods --field-selector=status.phase!=Running -A ?

@mdebruyn-trip
Copy link

Another version:
kubectl delete pod $(kubectl get pods | awk '/Evicted/ {print $1}')

@leodc
Copy link

leodc commented Feb 12, 2020

One to delete on a specific namespace:
NAMESPACE=jenkins sh -c 'for pod in $(kubectl get pods -n "$NAMESPACE"|grep Evicted|awk "{print $1}"); do kubectl delete pod -n "$NAMESPACE" $pod; done'

@denysvitali
Copy link

kubectl get po -A --all-namespaces -o json | jq  '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete po \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c

@Leo7654
Copy link

Leo7654 commented Mar 18, 2020

kubectl get po -A --all-namespaces -o json | jq  '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete po \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c

Thank you!

@qinghon
Copy link

qinghon commented Mar 25, 2020

kubectl get po -A |grep Evicted|awk '{print "kubectl delete po -n ",$1,$2}'|bash -x

@ksaliya
Copy link

ksaliya commented Mar 29, 2020

kubectl delete pod $(kubectl get pods | grep Evicted | awk '{print $1}')

@qinghon
Copy link

qinghon commented Mar 31, 2020

kubectl delete pod $(kubectl get pods | grep Evicted | awk '{print $1}')

not have namespace,It's just for default namespace ,sad for you.

@ksaliya
Copy link

ksaliya commented Apr 1, 2020

kubectl delete pod $(kubectl get pods | grep Evicted | awk '{print $1}')

not have namespace,It's just for default namespace ,sad for you.

How hard is it to include the namespace with the flag -n

@lhw
Copy link

lhw commented Jun 15, 2020

And using just kubectl kubectl delete pod $(kubectl get pod -o jsonpath="{range .items[*]}{.metadata.name}{\" \"}{end}" --field-selector="status.phase==Failed")

@mcat56
Copy link

mcat56 commented Aug 10, 2020

How to reproduce this command for powershell?

@willemm
Copy link

willemm commented Aug 14, 2020

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

@duyleekun
Copy link

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

Your answer worth the scroll :)) Should be the top answer

@php-workx
Copy link

Everybody who's a little concerned about running delete commands can use this the verify which pods would be affected:

kubectl get pod -A --field-selector="status.phase==Failed"

For kubectl get and kubectl delete you can use -A if you want to run it for all namespaces, otherwise a -n NAMESPACE to limit the scope to one specific namespace.

@yurikilian
Copy link

like @qinghon:
kubectl get po -A | awk '/Evicted|Completed/ {print "kubectl delete po -n ",$1,$2}'|bash -x
The gap about this approach without grep is that you filter for the status after getting all the input

@Talador12
Copy link

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

💯

@signalw
Copy link

signalw commented Apr 8, 2021

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

👍

@clementohNZ
Copy link

clementohNZ commented Apr 24, 2021

Just created a handy function. You can add this to your .zshrc or .bash_profile and it will just clean everything for you. Just add your namespaces to the array.

function deleteEvictedPods() {
    environments=(
        development
        staging
        production
        traefik
        kube-system
        kube-public
        kube-node-lease
        default
    )

    for environment in "${environments[@]}"
    do
        echo "==================================================================================="
        echo "Removing evicted pods for namespace: \"${environment}\""
        echo "---------------------------------------------------"

        for each in $(kubectl get pods -n ${environment} | grep Evicted | awk '{print $1}');
        do
            kubectl delete pods $each -n ${environment}
        done
        echo "==================================================================================="
        echo "\n"
    done
}

output

===================================================================================
===================================================================================
Removing evicted pods for namespace: "development"
---------------------------------------------------
pod "example-frontend-primary-757586fc7c-2zrp8" deleted
pod "example-frontend-primary-757586fc7c-54k6h" deleted
pod "example-frontend-primary-757586fc7c-62g5n" deleted
pod "example-frontend-primary-757586fc7c-6v4kv" deleted
pod "example-frontend-primary-757586fc7c-7jh6q" deleted
pod "example-frontend-primary-757586fc7c-89bvw" deleted
pod "example-frontend-primary-757586fc7c-c7mvt" deleted
pod "example-frontend-primary-757586fc7c-cc5lt" deleted
pod "example-frontend-primary-757586fc7c-jhmm6" deleted
pod "example-frontend-primary-757586fc7c-k5xfn" deleted
pod "example-frontend-primary-757586fc7c-pdfgk" deleted
pod "example-frontend-primary-757586fc7c-qhjnx" deleted
pod "example-frontend-primary-757586fc7c-r77hj" deleted
pod "example-frontend-primary-757586fc7c-v48h2" deleted
pod "example-service-feature-flags-55f8fb4858-4hmq6" deleted
pod "example-service-feature-flags-55f8fb4858-5vk44" deleted
pod "example-service-feature-flags-55f8fb4858-mkrx9" deleted
===================================================================================
===================================================================================

===================================================================================
===================================================================================
Removing evicted pods for namespace: "staging"
---------------------------------------------------
No resources found in staging namespace.
===================================================================================
===================================================================================

===================================================================================
===================================================================================
Removing evicted pods for namespace: "production"
---------------------------------------------------
No resources found in production namespace.
===================================================================================
===================================================================================

===================================================================================
===================================================================================
Removing evicted pods for namespace: "traefik"
---------------------------------------------------
pod "example-traefik-ingress-controller-df6f59b47-8k6qx" deleted
pod "example-traefik-ingress-controller-df6f59b47-kfjbr" deleted
pod "example-traefik-ingress-controller-df6f59b47-m59s4" deleted
pod "example-traefik-ingress-controller-df6f59b47-mrq8p" deleted
pod "example-traefik-ingress-controller-df6f59b47-nbsjc" deleted
pod "example-traefik-ingress-controller-df6f59b47-pfqbg" deleted
pod "example-traefik-ingress-controller-df6f59b47-vsnqw" deleted
pod "example-traefik-ingress-controller-df6f59b47-wzd6g" deleted
pod "example-traefik-ingress-controller-df6f59b47-xpq9x" deleted
===================================================================================
===================================================================================

===================================================================================
===================================================================================
Removing evicted pods for namespace: "kube-system"
---------------------------------------------------
===================================================================================
===================================================================================

===================================================================================
===================================================================================
Removing evicted pods for namespace: "kube-public"
---------------------------------------------------
No resources found in kube-public namespace.
===================================================================================
===================================================================================

===================================================================================
===================================================================================
Removing evicted pods for namespace: "kube-node-lease"
---------------------------------------------------
No resources found in kube-node-lease namespace.
===================================================================================
===================================================================================

===================================================================================
===================================================================================
Removing evicted pods for namespace: "default"
---------------------------------------------------
No resources found in default namespace.
===================================================================================
===================================================================================

@meeDeepakOfficial
Copy link

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

Thanks

@anmolnagpal
Copy link

kubectl get pods --all-namespaces -o json | jq '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete pods \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c

@kodeine
Copy link

kodeine commented Apr 19, 2022

i think the most simple command is
kubectl delete pods -A --field-selector=status.phase=Failed

@mudssrali
Copy link

mudssrali commented Apr 19, 2022

This works for me

kubectl delete pods $(kubectl get pods | grep [pod name] | grep Evicted | awk '{print $1}')

@MattyKuzyk
Copy link

Why doesn't Kubernetes clean up Evicted pods by itself? I only notice it happen sometimes.

@SunnyFenng
Copy link

SunnyFenng commented Jul 11, 2022

kubectl get pod -A | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs -n 2 kubectl delete pod

@magf
Copy link

magf commented Aug 4, 2022

i think the most simple command is
kubectl delete pods -A --field-selector=status.phase=Failed

best solution! 👍

@luizvinhas
Copy link

i think the most simple command is kubectl delete pods -A --field-selector=status.phase=Failed

works for me

@andyfcx
Copy link

andyfcx commented Dec 5, 2022

kgpa | grep Evicted | awk '{print $2}' | xargs kubectl delete pod --force

@andyfcx
Copy link

andyfcx commented Dec 6, 2022

kgpa | grep -v Running | awk '{print $2}' | xargs kubectl delete pod --force

@karatedog
Copy link

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

The original question is about to delete "Evicted" pods, which is a subset of "Failed". Unfortunately there is no state field for pods that are in the Running state.

@VictoriaYaa
Copy link

kubectl get po -A --all-namespaces -o json | jq  '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete po \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c

Thank you!

@promisepreston
Copy link

i think the most simple command is kubectl delete pods -A --field-selector=status.phase=Failed

Works for me too, for deleting failed pods in all namespaces

@albertoeks
Copy link

Did you know that you can use the --field-selector option for kubectl delete as well?

kubectl delete pod --field-selector="status.phase==Failed"

Great answer, thanks!

@recumbent
Copy link

Why doesn't Kubernetes clean up Evicted pods by itself? I only notice it happen sometimes.

My understanding is that there is a threshold for cleaning up - when the number of failed hits that threshold then clean up will happen - the challenge is that the default for that is 12500 (twelve thousand five hundred). The purpose of the threshold is to allow for review of the reasons for failure and I can see that in a large system that might almost be a reasonable number.

That threshold can be changed - I'm not sure what a sensible number would look like for a small cluster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment