Q: What happens when you delete a namespace that has resources in it?
When you delete a Kubernetes namespace, the API server marks it Terminating and the namespace controller deletes every namespaced object inside it, then removes the namespace itself.
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: When you <strong>delete a Kubernetes namespace</strong>, the API server marks it <code>Terminating</code> and the namespace controller deletes every namespaced object inside it, then removes the namespace itself. Cluster-scoped objects are not deleted, and storage outcomes depend on reclaim policy. Interviewers ask this because deletion is destructive and sometimes hangs forever: you need to explain finalizers, aggregated API failures and why force-removing a finalizer is a last resort.
Mental model: what the namespace controller does
Deletion is asynchronous and ordered by object finalizers, not instant.
- Phase change:
kubectl delete ns demosets adeletionTimestampand phaseTerminating; new objects cannot be created in it. - Cascade: the controller discovers every namespaced resource type and deletes all instances: Pods, Deployments, Services, ConfigMaps, Secrets, PVCs, Roles, custom resources.
- Pods: get SIGTERM and honour
terminationGracePeriodSeconds(default 30s). - Finalize: when empty, the
kubernetesfinalizer inspec.finalizersis removed and the namespace disappears.
Hands-on: inspect before and after deleting
Know what you are about to destroy.
kubectl api-resources --verbs=list --namespaced -o name \
| xargs -n1 kubectl get -n demo --ignore-not-found --show-kind
kubectl get pvc -n demo
kubectl get pv -o custom-columns=NAME:.metadata.name,RECLAIM:.spec.persistentVolumeReclaimPolicy,CLAIM:.spec.claimRef.name
kubectl delete ns demo --wait=false
kubectl get ns demo -w
- Inventory: list all namespaced resource types, not only
kubectl get all, which omits many kinds. - Storage: check PVCs and the PV reclaim policy:
Deleteremoves the backing volume,Retainkeeps it asReleased. - Delete:
kubectl delete ns demo --wait=falsereturns immediately; omit the flag to block. - Watch:
kubectl get ns demo -w.
Verify cleanup and what survives
Not everything is removed.
- Gone: every namespaced object, including Secrets and RoleBindings in the namespace.
- Survives: cluster-scoped objects such as ClusterRoles, ClusterRoleBindings you created for it, CRDs, StorageClasses, and PVs with
Retain. - Cloud resources: LoadBalancer Services release their cloud load balancers via the controller, which can take a minute; PVs with
Deleteremove disks. - Confirm:
kubectl get nsand check cloud console or CLI for orphaned volumes and load balancers.
Failure mode: stuck in Terminating
The most common production complaint.
kubectl get ns demo -o jsonpath='{range .status.conditions[*]}{.type}={.status} {.message}{"\n"}{end}'
kubectl get apiservice | grep -v True
# last resort, on the specific stuck object:
kubectl patch <kind>/<name> -n demo --type=merge -p '{"metadata":{"finalizers":[]}}'
- Read conditions:
kubectl get ns demo -o jsonpath='{.status.conditions}'names the blocker, such asNamespaceContentRemainingorNamespaceDeletionDiscoveryFailure. - Resource finalizers: a custom resource whose controller was uninstalled first keeps its finalizer, and nothing removes it.
- Broken APIService: an unavailable aggregated API (for example metrics) stops discovery, so deletion cannot enumerate types:
kubectl get apiservice | grep False. - Fix the cause: reinstall the controller or restore the APIService; remove a finalizer from the specific object only if you accept leaking external resources.
Production practice: make deletion safe
Reduce blast radius.
- RBAC: restrict
deleteon namespaces to a small group. - Labels and policy: use admission policy to block deletion of namespaces labeled
protected=true. - Backups: take Velero or snapshot backups before removing stateful namespaces; set PV reclaim to
Retainfor important data. - GitOps: delete through Git and let Argo CD or Flux prune, with a review step.
- Deleting a namespace marks it Terminating and cascades deletion of every namespaced object, but leaves cluster-scoped resources.
- I inventory resources and PV reclaim policies first, then watch conditions if it hangs on finalizers or a broken APIService.
- I protect it with RBAC, admission policy, backups and Git-driven deletion.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Using kubectl get all to see what will be deleted It lists only a few core kinds; Secrets, ConfigMaps, PVCs and custom resources are omitted.
- Force-removing finalizers immediately Finalizers exist to clean external resources; stripping them can orphan disks, load balancers or DNS records.
- Forgetting PV reclaim policy With <code>Delete</code> the backing volume is destroyed along with the PVC; with <code>Retain</code> it lingers and costs money.
- Uninstalling operators before deleting their namespaces Custom resources keep finalizers that only the operator can clear, leaving the namespace stuck in Terminating.