Skip to content

Instantly share code, notes, and snippets.

@usrbinkat
Created June 13, 2024 18:53
Show Gist options
  • Save usrbinkat/35235f83c85edd35ddaf5cd16df5e0c8 to your computer and use it in GitHub Desktop.
Save usrbinkat/35235f83c85edd35ddaf5cd16df5e0c8 to your computer and use it in GitHub Desktop.
Delete Stuck Namespace

Delete Stuck Namespace

Here be dragons. Use with extreme caution.

About

This script is designed to accept namespaces as a list of arguments and will perform a namespace child resource delete loop before terminating the namespace and any finalizers.

Motivation

Having Google'd the classic StackOverflow Deleted Namespace Stuck In Pending question for the five thousandth time I decided to make this a script in a gist.

#!/bin/bash
# Delete all resources in namespace
delete_resources_in_namespace() {
local namespace=$1
echo "Deleting all resources in namespace: $namespace"
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n 1 kubectl delete --all -n "$namespace" --ignore-not-found --wait
}
# Remove finalizers and delete stuck namespace
delete_namespace() {
local namespace=$1
echo "Removing finalizers and deleting namespace: $namespace"
kubectl get namespace "$namespace" -o json \
| jq 'del(.spec.finalizers)' \
| kubectl replace --raw "/api/v1/namespaces/$namespace/finalize" -f -
kubectl delete namespace "$namespace"
echo "Waiting for namespace $namespace to be deleted..."
while kubectl get namespace "$namespace" &>/dev/null; do
sleep 1
done
echo "Namespace $namespace has been deleted."
}
# Check for at least one namespace provided as argument
if [ $# -eq 0 ]; then
echo "Usage: $0 <namespace1> <namespace2> ... <namespaceN>"
exit 1
fi
# Loop through and delete list of namespaces
for namespace in "$@"; do
delete_resources_in_namespace "$namespace"
delete_namespace "$namespace"
done
@usrbinkat
Copy link
Author

Ooh, thank you for the tip @v0lkan if I run into it I'll make a point to incorporate this technique into the script as well. Awesome stuff!

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