Skip to content

Namespace stuck in Terminating: find the cause, don't force it

Kubernetestested on RKE2 v1.37.2

A namespace only disappears once everything in it is deleted. If it's stuck in "Terminating", the namespace controller writes the reason into its conditions – usually a finalizer nobody removes, or an aggregated API that doesn't answer.

Symptom

kubectl get ns tag16
NAME    STATUS        AGE
tag16   Terminating   2m13s

AGE counts from creation, not from deletion. Without --wait=false, kubectl delete namespace waits forever.

Diagnosis

The conditions with status True show what's holding the namespace.

kubectl get ns tag16 -o json | jq -r '.status.conditions[]
  | select(.status=="True") | "\(.type): \(.message)"' | fold -w 86
NamespaceDeletionDiscoveryFailure: Discovery failed for some groups, 1 failing: unable
 to retrieve the complete list of server APIs: metrics.k8s.io/v1beta1: stale GroupVers
ion discovery: metrics.k8s.io/v1beta1
NamespaceContentRemaining: Some resources are remaining: configmaps. has 1 resource in
stances
NamespaceFinalizersRemaining: Some content in the namespace has finalizers remaining:
labor.test/aufraeumen in 1 resource instances

Causes

NamespaceFinalizersRemainingA finalizer no controller removes

An object in the namespace carries a finalizer, and the responsible controller doesn't remove it – because it's missing or stuck. The object stays, and so does the namespace.

NamespaceDeletionDiscoveryFailureAPIService without a backend

If an aggregated API is unreachable, the controller can't tell whether objects are still there and won't release the namespace. The APIService is cluster-wide – every namespace you delete during that time gets stuck.

kubectl get apiservice | grep -v Local | tr -s ' '
NAME SERVICE AVAILABLE AGE
v1beta1.metrics.k8s.io kube-system/rke2-metrics-server False (MissingEndpoints) 5h30m

Fix

Start with the finalizer – for a real one, first check which controller sets it and why it doesn't remove it. If the namespace still hangs on discovery, bring the aggregated API's backend back. The namespace controller retries with backoff – in the course the namespace was gone after 35 s.
kubectl -n tag16 patch configmap test3 --type=merge \
  -p '{"metadata":{"finalizers":null}}'
kubectl -n kube-system scale deploy rke2-metrics-server --replicas=1
kubectl -n kube-system rollout status deploy/rke2-metrics-server
kubectl get ns tag16
configmap/test3 patched
deployment.apps/rke2-metrics-server scaled
deployment "rke2-metrics-server" successfully rolled out
Error from server (NotFound): namespaces "tag16" not found

Don't: Removing the kubernetes finalizer on the namespace itself via /finalize. The namespace is gone, but its objects not necessarily: they stay orphaned in etcd and reappear as soon as someone creates a namespace with the same name.

▶
Break it yourself and find it – free
Debugging Kubernetes, day 16: requests rejected by webhooks, missing APIServices and stuck namespaces – with a drill to reproduce each one.
Go to day 16 →

FAQ

How do I find all stuck namespaces?

kubectl get ns --field-selector=status.phase=Terminating lists every namespace in Terminating.

Why is the namespace still stuck when the objects are gone?

Then usually only NamespaceDeletionDiscoveryFailure is left in the conditions. kubectl get apiservice | grep False shows the API group whose backend is missing.

Related error patterns

More