Sub topic 13
Deleting namespaces and handling Terminating
This chapter assumes you can run kubectl, know that finalizers exist on Kubernetes objects, and have created a namespace such as team-a in earlier chapters.
What deletion does
Deleting a namespace deletes everything inside it: Pods, Deployments, Services, Secrets, ConfigMaps, Roles, quotas, and custom resources. There is no confirmation prompt and no undo.
kubectl delete namespace team-aThe command usually returns after the namespace is marked for deletion, or it waits until cleanup finishes. The namespace goes through the Terminating phase while the namespace controller removes its contents. Cluster-scoped objects are not deleted. This includes PersistentVolumes, which can outlive the namespace depending on their reclaim policy.
Why a namespace gets stuck
The namespace controller deletes every namespaced object, then removes the kubernetes finalizer from spec.finalizers. The namespace disappears only after that. It stays in Terminating when:
- An object has a finalizer that no controller is handling, often a custom resource whose operator was uninstalled first.
- An aggregated API service, such as a metrics API, is unavailable. The controller cannot list all resource types, so it cannot confirm the namespace is empty.
- A webhook rejects the delete requests.
- A Pod cannot finish terminating, for example because its node is gone.
Diagnose first
The namespace status explains the cause in its conditions.
kubectl get namespace team-a --output jsonpath='{.status.conditions}'Look at the condition types:
| Condition | Meaning |
|---|---|
| `NamespaceDeletionDiscoveryFailure` | Some API group could not be queried, often an unavailable APIService |
| `NamespaceDeletionContentFailure` | Some objects could not be deleted |
| `NamespaceContentRemaining` | Objects still exist |
| `NamespaceFinalizersRemaining` | Objects still carry finalizers |
To find what remains, list every namespaced resource type:
kubectl api-resources --verbs=list --namespaced --output name \
| xargs -n 1 kubectl get --show-kind --ignore-not-found --namespace team-aIf the discovery condition names an unavailable API, check it with kubectl get apiservices and look for entries where AVAILABLE is False. Fixing or removing that APIService often lets deletion continue.
Fix the blocker, then force as a last resort
Prefer fixing the cause. Reinstall the missing controller so it can process its finalizers, or repair the failing webhook.
If the owning controller is gone for good, clear the finalizers on the remaining object:
kubectl patch widget my-widget --namespace team-a \
--type=merge --patch '{"metadata":{"finalizers":null}}'Here widget stands for the custom resource type you found in the listing above.
Only when no object remains and the namespace is still stuck, clear the namespace's own finalizer through the finalize endpoint. This example needs jq:
kubectl get namespace team-a --output json \
| jq '.spec.finalizers = []' \
| kubectl replace --raw "/api/v1/namespaces/team-a/finalize" -f -Common mistakes
- Deleting a CRD or operator before deleting the namespaces that use its resources.
- Deleting
kube-systemor other system namespaces. Never do this. - Assuming a PersistentVolume is removed with the namespace. Check its reclaim policy.
- Repeating
kubectl deletewhile the namespace isTerminating. It does nothing new, so diagnose instead.
Exercise
A namespace has been in Terminating for an hour. Its status shows NamespaceDeletionDiscoveryFailure. What is the most likely cause, and what do you check first?
Show answer
The namespace controller could not query some API group, usually because an aggregated APIService is unavailable. Run kubectl get apiservices and look for entries where AVAILABLE is False. Repair or remove that APIService so the controller can list every resource type, then deletion proceeds. Forcing the finalize endpoint is a last resort.