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.
- API clientRequest deletionAPI serverThe server sets deletionTimestamp and keeps the object while the finalizer is present.
- Cleanup controllerComplete the required cleanupExternal resourceThe responsible controller can verify that the external obligation is satisfied.
- Cleanup controllerRemove its finalizerAPI serverWith all finalizers cleared, the object can be deleted.
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.
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.
- Before
- Sent to the API server
- After
- Accepted, finalizer still 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.
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 finalizerExplore 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.
Record the owning UID
- Deployment UID AReplicaSetowns by UID
- ReplicaSetPodowns by UID
The ReplicaSet references Deployment UID A; labels do not define this lifecycle edge.
Choose a propagation policy
- Garbage collectorReplicaSetcollect eligible dependent
Deleting UID A initiates background, foreground or orphan behavior according to the selected policy.
Observe dependent cleanup
- Garbage collectorReplicaSetcollect eligible dependent
- ReplicaSetPodowns by UID
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-deletionkubectl --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=30sUse 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.
Find clients using the old version
- Old API clientAPI serverold request version
Identify controllers, manifests and webhook rules using the deprecated endpoint before an upgrade.
Move clients and representations
- Updated clientAPI servernew request version
- API serverStored objectsconvert and persist
Update clients and test conversion while old and new supported versions are still available.
Verify the stored version and removal path
- API serverStored objectsconvert and persist
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