Skip to content

Instantly share code, notes, and snippets.

@leonsteinhaeuser
Created July 5, 2023 08:52
Show Gist options
  • Save leonsteinhaeuser/524dd03e6c74229dd0dc658e569c06fc to your computer and use it in GitHub Desktop.
Save leonsteinhaeuser/524dd03e6c74229dd0dc658e569c06fc to your computer and use it in GitHub Desktop.
Force delete all "Terminating" pods in Kubernetes (not namespace aware)
#!/bin/bash
# identify kubernetes config file
k_config_path=~/.kube/config
if [ "$KUBE_CONFIG" != "" ]; then
k_config_path=$KUBE_CONFIG
fi
# identify kubernetes context
k_context=""
if [ "$KUBE_CONTEXT" != "" ]; then
k_context=$KUBE_CONTEXT
fi
# identify pods to force delete and prepare command
terminating_pods=$(kubectl --kubeconfig $k_config_path --context $k_context get pods -A | grep Terminating | awk '{print "-n " $1 " delete pods " $2 " --force --grace-period 0"}')
# execute command
while read -r line; do
kubectl --kubeconfig $k_config_path --context $k_context $line
done <<< "$terminating_pods"
@leonsteinhaeuser
Copy link
Author

The variables 'KUBE_CONFIG' and 'KUBE_CONTEXT' must be set if you want to change the default settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment