Skip to content

Instantly share code, notes, and snippets.

@yohangdev
Last active December 26, 2021 15:49
Show Gist options
  • Save yohangdev/e92bda172802ace9ed757c276ec7277c to your computer and use it in GitHub Desktop.
Save yohangdev/e92bda172802ace9ed757c276ec7277c to your computer and use it in GitHub Desktop.
Delete evicted pods from all namespaces (also ImagePullBackOff and ErrImagePull)
#!/bin/sh
# based on https://gist.github.com/ipedrazas/9c622404fb41f2343a0db85b3821275d
# https://github.com/kubernetes/kubernetes/issues/55051
# delete all evicted pods from all namespaces
kubectl get pods --all-namespaces | grep Evicted | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
# delete all stuck shutdown
kubectl get pods --all-namespaces | grep Shutdown | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
# delete all containers in ImagePullBackOff state from all namespaces
kubectl get pods --all-namespaces | grep 'ImagePullBackOff' | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
# delete all containers in ImagePullBackOff or ErrImagePull or Evicted state from all namespaces
kubectl get pods --all-namespaces | grep -E 'ImagePullBackOff|ErrImagePull|Evicted' | awk '{print $2 " --namespace=" $1}' | xargs kubectl delete pod
# delete all evicted pods from all namespaces (using field selector)
kubectl get pods --all-namespaces --field-selector='status.phase==Shutdown' -o custom-columns=NAMESPACE:metadata.namespace,NAME:metadata.name --no-headers | awk '{print $1, $2}' | xargs -n2 kubectl delete pod -n
kubectl get pods --all-namespaces --field-selector='status.phase==Failed' -o custom-columns=NAMESPACE:metadata.namespace,NAME:metadata.name --no-headers | awk '{print $1, $2}' | xargs -n2 kubectl delete pod -n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment