'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


Solution 1:[1]

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 )}'

Solution 2:[2]

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 )}'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 PANDURANG BHADANGE