πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Configuration advanced ⏱ 15 minutes K8s 1.28+

Finalizers and Ownership Guide

Understand Kubernetes finalizers and owner references for resource lifecycle management. Prevent resource leaks, implement cleanup logic.

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: Finalizers prevent resource deletion until cleanup logic completes. Add metadata.finalizers: ["my-operator.example.com/cleanup"] to resources that need external cleanup (cloud resources, DNS records). Your controller removes the finalizer after cleanup, allowing Kubernetes to delete the object.

The Problem

You delete a Namespace, CRD, or PVC and it gets stuck in Terminating forever. Or you delete a Deployment but its cloud load balancer remains orphaned. Finalizers and owner references control what happens when resources are deleted.

The Solution

Finalizers

apiVersion: v1
kind: ConfigMap
metadata:
  name: important-config
  finalizers:
    - "my-operator.example.com/cleanup"

Deletion flow:

  1. kubectl delete configmap important-config
  2. Kubernetes sets metadata.deletionTimestamp (marks for deletion)
  3. Object is NOT deleted β€” waiting for finalizers
  4. Your controller detects deletionTimestamp, performs cleanup
  5. Controller removes finalizer from metadata.finalizers
  6. All finalizers removed β†’ Kubernetes deletes the object

Owner References (Cascading Deletion)

apiVersion: v1
kind: Pod
metadata:
  name: web-abc123
  ownerReferences:
    - apiVersion: apps/v1
      kind: ReplicaSet
      name: web-5d8c4b7f6
      uid: "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
      controller: true
      blockOwnerDeletion: true

When the ReplicaSet is deleted, its owned Pods are garbage collected automatically.

Debugging Stuck Deletions

# Find what's blocking deletion
kubectl get namespace stuck-ns -o json | jq '.status.conditions'
kubectl get namespace stuck-ns -o json | jq '.spec.finalizers'

# Emergency: Remove finalizer to force deletion (USE WITH CAUTION)
kubectl patch namespace stuck-ns --type=json \
  -p='[{"op":"remove","path":"/spec/finalizers/0"}]'

# Find all resources in a stuck namespace
kubectl api-resources --verbs=list -o name | \
  xargs -I {} kubectl get {} -n stuck-ns --no-headers 2>/dev/null
graph TD
    DELETE[kubectl delete resource] -->|Set deletionTimestamp| MARK[Marked for deletion<br/>NOT yet deleted]
    MARK -->|Finalizer present?| CHECK{Has finalizers?}
    CHECK -->|Yes| CONTROLLER[Controller performs<br/>cleanup logic]
    CONTROLLER -->|Remove finalizer| EMPTY{Finalizers empty?}
    EMPTY -->|Yes| DELETED[βœ… Resource deleted]
    EMPTY -->|No| CONTROLLER
    CHECK -->|No| DELETED

Common Issues

Namespace stuck in Terminating

A resource in the namespace has a finalizer whose controller is gone. Find it: kubectl get all -n stuck-ns and check for finalizers. Remove the finalizer or delete the blocking resource.

CRD deletion stuck

CRD has customresourcecleanup finalizer and instances still exist. Delete all CR instances first, then the CRD.

Best Practices

  • Only add finalizers if you have a controller to remove them β€” otherwise resources get stuck forever
  • Owner references for cascading cleanup β€” child resources auto-delete with parent
  • blockOwnerDeletion: true β€” parent can’t be deleted until owned resources are gone
  • Never force-remove finalizers in production unless you understand the consequences
  • Namespace finalizers are special β€” Kubernetes handles them for resource cleanup

Key Takeaways

  • Finalizers prevent deletion until external cleanup completes
  • Owner references enable cascading garbage collection (delete parent β†’ delete children)
  • Stuck Terminating resources almost always have a finalizer whose controller is missing
  • Emergency fix: patch to remove finalizers β€” but understand what cleanup is being skipped
  • Always pair finalizers with a running controller that handles cleanup and removes them
#finalizers #owner-references #garbage-collection #lifecycle
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens