Appearance

Finalizers, garbage collection, and API evolution

Trace what keeps a deleting object around, what gets cleaned up with its owner, and what breaks when an API version is removed.

What you will understand

  • Explain how deletionTimestamp and finalizers cooperate.
  • Predict dependent cleanup from owner references and the deletion propagation policy.
  • Identify clients and stored objects that need attention before an API version is removed.

Builds on: Watches and server-side apply

What can hold up object deletion

An object represents a volume that lives outside Kubernetes. Deleting the API record immediately could leave that storage running with nobody responsible for it. A finalizer is a key on an object that keeps it present during deletion. The API server sets deletionTimestamp. A controller that understands the finalizer observes the request, performs or verifies external cleanup, and removes its key. The API can finish deleting the object when the finalizers are cleared.

In the scene, delete an object with a finalizer, then follow the controller that removes it. The key is a barrier, and actual cleanup depends on a working controller. If the controller has lost permissions or the external system is unavailable, the object may remain in a deleting state.

How does the controller finish a pending deletion?

A finalizer keeps the object available during cleanup. The controller removes that key after the obligation is satisfied.

  1. API clientRequest deletion
    API serverThe server sets deletionTimestamp and keeps the object while the finalizer is present.
  2. Cleanup controllerComplete the required cleanup
    External resourceThe responsible controller can verify that the external obligation is satisfied.
  3. Cleanup controllerRemove its finalizer
    API serverWith all finalizers cleared, the object can be deleted.
Follow the sequence

A finalizer keeps the object available during cleanup. The controller removes that key after the obligation is satisfied.

An external cleanup obligation illustrates the mechanism; the lab ConfigMap deliberately has none.

Inspect the pending deletion

Delete the lab ConfigMap while it has a teaching finalizer. Follow the API response, inspect the remaining object, and see when it disappears.

1 / 4

From kubectlTo API server

Request deletion with the key present

The lab operator sends a DELETE request. The API server accepts the request and sets metadata.deletionTimestamp. The finalizer still gates removal.

Delete request
Before
Sent to the API server
After
Accepted, finalizer still present
Request deletion with the key present

The lab operator sends a DELETE request. The API server accepts the request and sets metadata.deletionTimestamp. The finalizer still gates removal.

The lab’s finalizer is a marker with no controller or external cleanup behind it. A production controller normally verifies the cleanup associated with its own finalizer before removing that key.

What should you check before removing an unfamiliar finalizer?
Reveal prediction answer

Identify the controller and the cleanup associated with the key. Removing it early can leave resources behind that Kubernetes can no longer track through the deleted object.

Read metadata.deletionTimestamp and metadata.finalizers after deletion, then verify the object is gone after releasing the teaching key.

Inspect and release the practice finalizer
Explore another case

Open Inspect the pending deletion to follow the lab ConfigMap through two reads. The first read shows deletionTimestamp and the finalizer key. After the teaching key is released, the next read can confirm that the object is gone.

How Kubernetes finds dependents

Garbage collection deals with dependencies between Kubernetes objects. An owner reference records the owning object’s identity, including its UID. A ReplicaSet can disappear and another object can be created under the same name, but the replacement has a different UID. The old dependents do not acquire a new owner just by matching its name. Deletion propagation controls whether dependents are cleaned up in the background, deleted before a foreground owner deletion completes when they block it, or left as orphans.

How does garbage collection know what belongs to an owner?

An owner reference includes a UID. The deletion propagation policy determines whether to collect the dependent objects or leave them orphaned.

  1. Record the owning UID

    The ReplicaSet references Deployment UID A; labels do not define this lifecycle edge.

  2. Choose a propagation policy

    Deleting UID A initiates background, foreground or orphan behavior according to the selected policy.

  3. Observe dependent cleanup

    Eligible ReplicaSets and Pods are removed through their owner graph; unrelated objects remain.

The diagram summarizes garbage collection; foreground blocking and cache timing affect the exact removal sequence.

Explore another case

Inspect an object held by a finalizer

Save the ConfigMap as held.yaml and try it only in the disposable cluster. The exercise adds a finalizer deliberately left without a controller. With --wait=false, the delete command returns while the finalizer still holds the object. Inspect the timestamp and key. The final patch removes the artificial hold. In a real cluster, identify which cleanup the controller owes before considering manual removal.

apiVersion: v1
kind: ConfigMap
metadata:
  name: m14-held
  namespace: kv-lab
  labels:
    lab: m14
  finalizers:
    - labs.kv.example/hold # No controller implements this training key.
data:
  purpose: inspect-deletion
kubectl --context kind-kv-lab -n kv-lab apply -f held.yaml
kubectl --context kind-kv-lab -n kv-lab delete configmap m14-held --wait=false
kubectl --context kind-kv-lab -n kv-lab get configmap m14-held -o yaml
# Only the artificial hold is removed in this isolated exercise.
kubectl --context kind-kv-lab -n kv-lab patch configmap m14-held --type=merge -p '{"metadata":{"finalizers":[]}}'
kubectl --context kind-kv-lab -n kv-lab wait --for=delete configmap/m14-held --timeout=30s

Use the API mechanics lab to inspect the delete request, then confirm the object has actually gone.

Prepare to retire an API version

An API version also has a lifecycle. A newer version can serve objects under a different representation while old clients still send requests to the old endpoint. Before removing a version, identify manifests, scripts, and controllers using it. Update their requests and check how existing objects are stored. For a custom resource, migrate persistence and check the CRD’s stored versions as part of retiring a storage version. Follow the API evolution scene to distinguish serving a version from storing data in that version.

How can an API version be retired safely?

Move callers to a supported version and migrate persisted objects when necessary. Check both before removing an old version.

  1. Find clients using the old version

    Identify controllers, manifests and webhook rules using the deprecated endpoint before an upgrade.

  2. Move clients and representations

    Update clients and test conversion while old and new supported versions are still available.

  3. Verify the stored version and removal path

    Check stored-version migration and release policy before disabling the old representation.

Specific removal releases and CRD conversion mechanics must be checked for the chosen resource and upgrade path.

Explore another case

Check your understanding

A deleting object disappeared after someone removed its only finalizer. What has actually been confirmed?

The API object has been deleted. Verify the state of the storage or other external resource separately. The key was removed manually, so the recorded object’s disappearance does not prove that its controller performed cleanup.

Go to the source

Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu