Skip to content

Instantly share code, notes, and snippets.

@jonstacks
Created January 9, 2020 04:08
Show Gist options
  • Save jonstacks/876ab7935a43260f677f6d7be4aa8fba to your computer and use it in GitHub Desktop.
Save jonstacks/876ab7935a43260f677f6d7be4aa8fba to your computer and use it in GitHub Desktop.
Purge a kubernetes namespace but only if it is empty
#!/bin/bash
set -e
NAMESPACE="$1"
echo "Checking if we can purge namespace $NAMESPACE"
OUTPUT=$(kubectl -n "$NAMESPACE" get all 2>&1)
echo "$OUTPUT"
case "$OUTPUT" in
"No resources found.")
kubectl delete namespace "$NAMESPACE"
exit 0
;;
*)
echo ""
echo "Can't delete namespace $NAMESPACE as it is not empty"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment