A Quick way to delete namespaces on Kubernetes, that are stuck in terminating state.
Disclaimer: Only use this, if the stuck namespace is completely empty, as otherwise you will have dangling ressources.
- Clone the repo on the host, where you are interacting with your cluster.
- cd into the cloned repo and
sudo chmod +x unstuck-ns.sh
to make it executable. - Run
./unstuck-ns.sh
to get rid of the annoying namespace. - Rerun, if you have other namespaces stuck.
If you need to run the kubectl command with sudo rights (e.g. if you have a k3s-cluster) you can simple add the flag --use_sudo to the command.
./unstuck-ns.sh --use_sudo
If you need to use this often, you can create an alias like u-ns.
- Clone repo,
sudo chmod +x
the script (if not done already). - Move it
sudo mv unstuck-ns.sh /usr/local/bin/unstuck-ns.sh
. - In your .bashrc add:
# add custom script for unstucking NS
alias u-ns="/usr/local/bin/unstuck-ns.sh"
source ~/.bashrc
to reload settings.
The script is based on the following snippet
NS=`kubectl get ns |grep Terminating | awk 'NR==1 {print $1}'` && kubectl get namespace "$NS" -o json | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" | kubectl replace --raw /api/v1/namespaces/$NS/finalize -f -
Found on StackOverflow