Skip to content

Instantly share code, notes, and snippets.

@rahulkj
Last active May 30, 2023 03:12
Show Gist options
  • Save rahulkj/3609d12ecc953bc11ae82e0d3729d02d to your computer and use it in GitHub Desktop.
Save rahulkj/3609d12ecc953bc11ae82e0d3729d02d to your computer and use it in GitHub Desktop.
Cleanup pods that are in a bad state
#!/bin/bash
CODES=("Error" "ContainerStatusUnknown" "Evicted" "CrashLoopBackOff" "Completed" "OOMKilled" "ImagePullBackOff" "ErrImagePull")
for code in "${CODES[@]}"; do
echo "Looking for pods that are in ${code} status"
PODS=($(kubectl get pods --all-namespaces | grep ${code} | awk '{print $2 "|" $1}'))
for pod in "${PODS[@]}"; do
echo "$pod" | awk -F"|" '{print $1 " --namespace=" $2}' | xargs kubectl delete pod;
done
echo "Moving onto the next"
echo "---------"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment