In k8s, garbage collection is a collective term used for the various mechanisms used for clean up of cluster resources (terminated pods, completed jobs, etc.).

By default, k8s uses cascading deletion for thorough cleanup, which relies on the owner-dependent nature of objects1. There are two modes of cascading operation:

  • “foreground”: The owner object enters a “deletion in progress” state and is not fully removed until dependents with blockOwnerDeletion=true (that are in the garbage controller cache) are deleted first.
  • “background” (default): The owner is deleted immediately, then dependents are removed asynchronously. Finalizers on both owner and dependents are honored.

There is also a third mode of operation which doesn’t qualify as “cascading”. With the “orphan” deletion strategy the owner is deleted (honoring its finalizers2), but dependents are preserved by removing their ownerReferences rather than deleting them.

Sources

Footnotes

  1. See Owners and Dependents in k8s.

  2. See Finalizers in k8s.