Skip to content

Instantly share code, notes, and snippets.

@rnemeth90
Created April 6, 2022 19:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rnemeth90/e83bb4c8808f0d28412cb40edb2487d3 to your computer and use it in GitHub Desktop.
Save rnemeth90/e83bb4c8808f0d28412cb40edb2487d3 to your computer and use it in GitHub Desktop.
Delete Terminating Kubernetes Namespaces with Bash
#!/bin/bash
namespaces=$(kubectl get ns --field-selector=status.phase==Terminating -o jsonpath='{range .items[*]}{.metadata.name}{"\n"}{end}')
if [ -z "$namespaces"]
then
echo "No namespaces to delete."
exit
else
for namespace in $namespaces
do
echo "[Removing Namespace]: $namespace"
kubectl get namespace $namespace -o json | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" | kubectl replace --raw /api/v1/namespaces/$namespace/finalize -f - > /dev/null 2>&1
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment