OpsCanary
Back to daily brief
kubernetesupgradesPractitioner

Mastering Kubernetes Garbage Collection: What You Need to Know

5 min read Kubernetes DocsApr 23, 2026
PractitionerHands-on experience recommended

Garbage collection exists to keep your Kubernetes cluster tidy and efficient. It solves the problem of resource bloat by automatically cleaning up objects that are no longer needed. This is especially important during upgrades, where orphaned resources can lead to confusion and wasted resources.

Kubernetes employs several mechanisms for garbage collection. Owner references indicate which objects depend on others, allowing the control plane to clean up related resources before deletion. There are two main types of cascading deletion: foreground and background. In foreground cascading deletion, the owner object first enters a deletion in progress state, while in background cascading deletion, the owner object is deleted immediately, and the garbage collector cleans up dependents in the background. You can also configure parameters like imageMaximumGCAge to control how long unused images are retained, and set limits on the number of dead containers per pod with MaxPerPodContainer.

In production, you need to be aware of some gotchas. Cross-namespace owner references are disallowed, which can lead to issues if not properly managed. Additionally, the kubelet only garbage collects containers it manages, and it doesn't track image usage across restarts. This means that if the kubelet restarts, the age tracking resets, delaying garbage collection. Be cautious when using external garbage collection tools, as they can interfere with kubelet behavior and remove containers that should exist.

Key takeaways

  • Understand owner references to manage resource dependencies effectively.
  • Utilize foreground and background cascading deletion to control resource cleanup.
  • Configure `imageMaximumGCAge` to limit unused image retention.
  • Monitor for cross-namespace owner references to avoid garbage collection issues.
  • Avoid external garbage collection tools to maintain kubelet integrity.

Why it matters

Effective garbage collection prevents resource bloat, ensuring your cluster runs smoothly and efficiently. This is critical during upgrades, where unmanaged resources can lead to performance degradation and confusion.

Code examples

shell
kubectl get events -A --field-selector=reason=OwnerRefInvalidNamespace
YAML
metadata.finalizers: foregroundDeletion
YAML
12h45m

When NOT to use this

You should avoid using external garbage collection tools, as these can break the kubelet behavior and remove containers that should exist.

Want the complete reference?

Read official docs

Test what you just learned

Quiz questions written from this article

Take the quiz →

Get the daily digest

One email. 5 articles. Every morning.

No spam. Unsubscribe anytime.