Skip to content

Instantly share code, notes, and snippets.

@davidmroth
Last active October 4, 2023 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmroth/6fb597ca011ce57f6aa01f5a5ed5f3b8 to your computer and use it in GitHub Desktop.
Save davidmroth/6fb597ca011ce57f6aa01f5a5ed5f3b8 to your computer and use it in GitHub Desktop.
Force delete all pods stuck in 'terminating' state
kubectl delete pods --field-selector=status.phase=Failed
kubectl get replicaset --all-namespaces -o=jsonpath='{range .items[?(@.spec.replicas==0)]}{.metadata.name}{"\t"}{.metadata.namespace}{"\n"}{end}' | awk '{print $1 " --namespace=" $2}' | xargs -n 2 -d '\n' bash -c 'kubectl delete replicaset $0 $1'
kubectl delete pod --grace-period=0 --force $(k get pods | grep -v Running | grep -v ContainerCreating | tail -n +2 | awk '{print $1}')
# Fix terminating NS
NS=<NS> kubectl get namespace "$NS" -o json | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" | kubectl replace --raw /api/v1/namespaces/$NS/finalize -f -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment