Gajukorse
Gajukorse

Reputation: 167

How to delete all the pods which are in Error/Init/CrashloopBckoff state at one shot

I have tried below command. but this command gets all the pods which are not running for some reason any other way to do this

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

Upvotes: 1

Views: 4419

Answers (3)

Prem Kumar
Prem Kumar

Reputation: 1

To delete all running pods in the current namespace, use the below command

kubectl get pods --field-selector 'status.phase=Failed' | awk '{if ($3 != "Running") system ("kubectl delete pods " $1 )}'

Upvotes: 0

PANDURANG BHADANGE
PANDURANG BHADANGE

Reputation: 69

You can use the below command for check the init pod with namespace like default-user and delete those pods.

kubectl get pods -A | grep default-user | grep -i "init:" | awk '{if ($4 != "Running") system ("kubectl -n " $1 " delete pods " $2 )}'

Upvotes: 0

Thomas
Thomas

Reputation: 12059

This might be what you are looking for, but uses awk and unix output piping.

kubectl get pods --field-selector 'status.phase=Failed' --all-namespaces | awk '{if ($4 != "Running") system ("kubectl -n " $1 " delete pods " $2 )}'

Upvotes: 3

Related Questions