KB: Kubernetes – Remove a namespace stuck in terminating state

In case of a k8s namespace stuck in the terminating state, you can use the two below commands to ensure that there is no remaining ressources and to remove the finalizers of the ressource:

1# declare the namespace that is stuck
2STUCKED_NS="stucked-namespace"

Ensure there is no remaining ressource(s):

1kubectl api-resources --verbs=list --namespaced -o name \
2  | xargs -n 1 kubectl get --show-kind --ignore-not-found -n $STUCKED_NS

And to remove the ressource finalizers:

1kubectl get namespace $STUCKED_NS -o json \
2  | tr -d "\n" | sed "s/\"finalizers\": \[[^]]\+\]/\"finalizers\": []/" \
3  | kubectl replace --raw /api/v1/namespaces/$STUCKED_NS/finalize -f -

Thanks to teoincontatto @ stackoverflow.com for this tip