⚡ ~/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 105 of 207 in Kubernetes
Staff SRE / Principal Architect [L3] Kubernetes Advanced Scenarios Staff SRE Scenario [L3]
🎯 Target Role / Context: Kubernetes Scenario #95 (L3) · Advanced Scenarios

Q: How does the Kubernetes garbage collector work?

The Kubernetes garbage collector is a controller in kube-controller-manager that deletes dependent objects once their owners are gone, using metadata.ownerReferences to build a dependency graph.

#Kubernetes #Advanced Scenarios #L3 #Container Orchestration #K8s
🎙️ Candidate Opening & Architectural Context
"The <strong>Kubernetes garbage collector</strong> is a controller in kube-controller-manager that deletes dependent objects once their owners are gone, using <code>metadata.ownerReferences</code> to build a dependency graph. It is why deleting a Deployment also removes its ReplicaSets and pods. Interviewers ask it to check that you separate three things: owner-based cascading deletion, kubelet housekeeping of images and containers, and TTL cleanup of finished Jobs. Knowing the three deletion propagation modes and how finalizers interact is what marks a senior answer."
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: The <strong>Kubernetes garbage collector</strong> is a controller in kube-controller-manager that deletes dependent objects once their owners are gone, using <code>metadata.ownerReferences</code> to build a dependency graph. It is why deleting a Deployment also removes its ReplicaSets and pods. Interviewers ask it to check that you separate three things: owner-based cascading deletion, kubelet housekeeping of images and containers, and TTL cleanup of finished Jobs. Knowing the three deletion propagation modes and how finalizers interact is what marks a senior answer.

1️⃣

The owner reference graph behind the garbage collector

Every dependent records who owns it; the collector watches that graph.

kubectl get rs -n prod -l app=my-app \
  -o custom-columns=NAME:.metadata.name,OWNER:.metadata.ownerReferences[0].name

kubectl get pod my-app-7d9c-xk2lp -n prod -o jsonpath='{.metadata.ownerReferences}'
  • ownerReferences: a list of apiVersion, kind, name and uid. Controllers set it automatically (Deployment to ReplicaSet to Pod).
  • Deletion trigger: when no owner with a matching UID exists any more, the dependent becomes garbage and is deleted.
  • Namespace rule: a namespaced owner cannot own objects in another namespace, and a namespaced dependent may have a cluster-scoped owner.
  • Not everything is owned: objects created by hand have no owner and are never collected automatically.
2️⃣

Three cascading deletion modes

The caller picks how dependents are removed.

# Default: background
kubectl delete deployment my-app -n prod

# Wait for pods to go first
kubectl delete deployment my-app -n prod --cascade=foreground

# Remove only the Deployment, keep ReplicaSet and pods running
kubectl delete deployment my-app -n prod --cascade=orphan
  • Background (default): owner is deleted immediately, then the collector removes dependents asynchronously.
  • Foreground: the owner gets a foregroundDeletion finalizer and stays visible until dependents with blockOwnerDeletion=true are gone.
  • Orphan: the owner is deleted and the ownerReferences are removed from dependents, leaving them running.
  • kubectl flag: --cascade=background|foreground|orphan (older versions used --cascade=false for orphan).
Pro Tip: Orphaning is a handy migration tool: a new controller with a matching selector can adopt the leftover pods or ReplicaSet without downtime.
Advertisement
3️⃣

Finalizers and objects stuck in Terminating

Finalizers delay actual removal until a controller finishes cleanup.

kubectl get ns demo -o jsonpath='{.metadata.finalizers}{"\n"}'
kubectl get all -n demo
kubectl get pods -A -o json | jq -r '.items[] | select(.metadata.deletionTimestamp) | .metadata.name'
  • Mechanism: deletion sets deletionTimestamp; the object stays until every entry in metadata.finalizers is removed.
  • Foreground dependency: an owner waits on blocking dependents, so a stuck child keeps the parent in Terminating.
  • Diagnosis: check finalizers and dependents before touching anything.
  • Last resort: patching finalizers away skips cleanup and may leak cloud resources, so fix the responsible controller first.
4️⃣

Related cleanup that is not the same collector

Clarify these separately in an interview.

  • Kubelet image GC: removes unused images when disk usage passes imageGCHighThresholdPercent (default 85) until it falls to the low threshold (80).
  • Kubelet container GC: cleans dead containers on the node.
  • PodGC controller: deletes terminated pods beyond --terminated-pod-gc-threshold and orphaned pods bound to missing nodes.
  • Job TTL: ttlSecondsAfterFinished in the Job spec removes finished Jobs and their pods through the TTL-after-finished controller.
5️⃣

Production practice

Design for predictable deletion.

  • Set ownerReferences in operators: custom controllers should use controllerutil.SetControllerReference so custom resources clean up their children.
  • Use foreground for ordered teardown: when dependents must finish before the owner disappears.
  • Guard Helm and GitOps: prune settings in Argo CD or Flux delete by ownership/labels, so review what a prune will remove.
  • Watch for leaks: alert on very old objects lacking owners, such as stray ConfigMaps or PVCs.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Before deleting a parent that is hard to recreate, use --cascade=orphan and verify ownerReferences on the survivors, then let a new controller adopt them."
⚡ 60-Second Elevator Pitch Talking Points
  • The garbage collector is a kube-controller-manager controller that deletes dependents whose owner (by ownerReferences UID) no longer exists.
  • Deletion can cascade in background, foreground or orphan mode, and finalizers can hold objects in Terminating until cleanup completes.
  • I inspect ownerReferences and finalizers when deletes hang, and I keep kubelet image GC and Job TTL separate in my mental model.
⛔ 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. Removing finalizers to unstick a delete without checking the controller It skips external cleanup and can leak load balancers, disks or other cloud resources.
  2. Assuming deleting a Deployment always deletes its pods immediately Background mode is asynchronous, and orphan mode keeps pods running.
  3. Confusing kubelet image garbage collection with ownerReference collection They run on different components with different triggers (disk thresholds vs object ownership).
  4. Setting cross-namespace owner references They are invalid for namespaced owners, so the dependent may be treated as having a missing owner and be collected.
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 →