Created
March 20, 2025 09:13
-
-
Save checco/92dfafc7262c544597a5e1e2dee19994 to your computer and use it in GitHub Desktop.
Delete a Kubernetes namespace stuck in Terminating status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
NAMESSPACE=`kubectl get namespaces --field-selector status.phase=Terminating --output json | jq '.items[].metadata.labels."kubernetes.io/metadata.name"' | tr -d '"'` | |
if [ -z ${NAMESPACE} ]; then | |
echo "ERROR: there is no stuck namespace in Terminating status" | |
exit 1 | |
else | |
echo "INFO: found ${NAMESPACE} namespace stuck in Terminating status" | |
echo "INFO: deleting the namespace" | |
kubectl get namespace "${NAMESPACE}" -o json \ | |
| tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" \ | |
| kubectl replace --raw /api/v1/namespaces/${NAMESPACE}/finalize -f - | |
if [ "$?" -eq "0" ]; then | |
echo "INFO: deleted ${NAMESPACE} stuck namespace" | |
else | |
echo "ERROR: something went wrong while deleting ${NAMESPACE} namespace" | |
exit 1 | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment