⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Kubernetes Interview Questions Scenario 148 of 207 in Kubernetes
Senior DevOps / SRE [L2] Kubernetes Additional Kubernetes Scenarios (Q101-Q200) Production Scenario [L2]
🎯 Target Role / Context: Kubernetes Scenario #138 (L2) · Additional Kubernetes Scenarios (Q101-Q200)

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.

#Kubernetes #L2 #Container Orchestration #K8s
🎙️ Candidate Opening & Architectural Context
"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."
Advertisement
⚡ Recommended Practice Lab

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.

1️⃣

Mental model: what the namespace controller does

Deletion is asynchronous and ordered by object finalizers, not instant.

  • Phase change: kubectl delete ns demo sets a deletionTimestamp and phase Terminating; 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 kubernetes finalizer in spec.finalizers is removed and the namespace disappears.
2️⃣

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: Delete removes the backing volume, Retain keeps it as Released.
  • Delete: kubectl delete ns demo --wait=false returns immediately; omit the flag to block.
  • Watch: kubectl get ns demo -w.
Advertisement
3️⃣

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 Delete remove disks.
  • Confirm: kubectl get ns and check cloud console or CLI for orphaned volumes and load balancers.
4️⃣

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 as NamespaceContentRemaining or NamespaceDeletionDiscoveryFailure.
  • 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.
Pro Tip: Avoid editing the namespace's <code>spec.finalizers</code> through the <code>/finalize</code> endpoint as a first move: it skips cleanup and can orphan external resources.
5️⃣

Production practice: make deletion safe

Reduce blast radius.

  • RBAC: restrict delete on 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 Retain for important data.
  • GitOps: delete through Git and let Argo CD or Flux prune, with a review step.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Before deleting a namespace, list every namespaced resource with <code>kubectl api-resources --verbs=list --namespaced</code> and check PV reclaim policies, because <code>kubectl get all</code> hides most kinds and <code>Delete</code> policy destroys the data."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. 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.
  2. Force-removing finalizers immediately Finalizers exist to clean external resources; stripping them can orphan disks, load balancers or DNS records.
  3. 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.
  4. Uninstalling operators before deleting their namespaces Custom resources keep finalizers that only the operator can clear, leaving the namespace stuck in Terminating.
Advertisement
📥 FREE DOWNLOAD · 101-PAGE COMPANION HANDBOOK
Studying for Kubernetes & SRE Technical Rounds?
Download the complete 100-question PDF field guide covering all 11 core modules with offline diagnostic runbooks.
📥 Download PDF (Free) Read Online Guide →
Want more Kubernetes scenarios?
Explore our complete collection of scenario-based Kubernetes interview runbooks.
Browse All Kubernetes Questions →