Skip to content

Held Deletions

A destroy needs the state. When the state is missing or unreadable, the controller cannot tell what the module created, and removing the finalizer would leave that infrastructure running with nothing tracking it. So it holds the deletion instead: no destroy runs, the finalizer stays, and the state backups stay with it. This page covers what triggers a hold, what ends one, and spec.deletionPolicy: Retain, which ends it without a destroy and without losing anything.

Ever applied

A missing state means two different things. For an object that never applied there is nothing to destroy, and the finalizer comes off at once. For one that did, the state was lost. The controller tells them apart with everApplied, which is true when any of these holds:

Signal Where it lives Survives clusterctl move
status.initialization.provisioned is true The object’s status No
The captf.io/applied: "true" marker The durable inputs Secret captf-inputs-*, set at the first successful apply or restore, never cleared Yes, it moves with the Secret
An applied record The applied Secret captf-applied-*; only a successful apply writes one Yes
Any state backup exists The captf-state-backup-* Secrets Yes (owned by the object)

With --state-backups=0 no backup is ever taken, so the last signal is absent; the other three still hold. The marker exists because status does not move: see clusterctl move.

The same test guards the ownerless release: a deleting object with no owner is dropped without a look at the state only if it never applied.

What holds a deletion

StateReadable becomes False and the deletion is held for these reasons:

Reason State Notes
StateLost No state Secret, and the object applied before The Secret was deleted by hand, or the namespace was
StateCorrupt The payload cannot be decoded, exceeds the reader’s caps, or has an unsupported version Restore a backup taken before it broke
StateEncrypted The state carries OpenTofu’s client-side encryption envelope; CAPTF cannot read it No backup exists (an unreadable state is never backed up)
StateInconsistent The chunks do not form one complete state A missing or duplicated chunk, or a stray Secret in the set
ApplyOutcomeUnknown No state, the object never applied before, and an apply Job ended without a result, vanished, or failed after its apply step without saving a state The apply may have created resources before any state was written; see Apply outcome unknown

The condition message adds how to leave the hold: the restore annotation, or spec.deletionPolicy: Retain. Each reason’s cause and repair are in Unreadable State.

StateLocked is not a hold. The state reads, so the destroy starts, and its Job waits lockTimeoutSeconds for a lock someone else holds and then fails. See Stale State Lock.

Apply outcome unknown

StateReadable=False/ApplyOutcomeUnknown is a hold for an object with no state that has not applied before (no applied marker, no applied record, no backup, not provisioned), when an apply Job ended without a runner result after its runner started (an OOM kill, a node loss), disappeared while it ran, or failed after its apply step without saving a state (the final state write to the backend failed; the runner pushes Terraform’s errored.tfstate first, so this is rare). That Job may have created resources before any state was written, so a second first apply would create a second set. The controller records the Job as captf.io/unconfirmed-apply, or keeps the attempt record’s captf.io/may-have-applied mark, on the durable inputs Secret, for every kind including immutable machines. No apply runs, and a deleting object keeps its finalizer. The message names the Job and the exits. Entering the state emits a StateLost Warning event. An object that applied before gets StateLost instead, which takes precedence.

A Job that never ran is not held: one whose pod never started (refused by a quota, unschedulable until its deadline) and that left the state lock as it found it (captf.io/state-lock-version) changed nothing, and the next apply starts. An object whose owner reference is gone also waits for bookkeeping to read its last apply before its finalizer can come off.

The exits:

  • Restore a backup (captf.io/restore-state). A successful restore newer than the newest apply also clears the record and the captf.io/may-have-applied mark.
  • Check the cloud, then set captf.io/confirm-no-resources=<job> on the object. It is consumed when the value names the recorded Job: the record is cleared and the annotation removed, and in the same pass a live object applies again and a deleting object drops its finalizer. A value naming another Job is ignored and logged.
  • deletionPolicy: Retain, when deleting.

What ends a hold

flowchart TD
    A[Deleting, state lost, unreadable or unconfirmed] --> B{deletionPolicy<br/>Retain?}
    B -- yes --> X[Retain: keep state, backups and inputs,<br/>finalizer off, InfrastructureRetained]
    B -- no --> C{restore-state names<br/>a complete backup?}
    C -- yes --> R[Restore Job]
    R --> S{state reads again?}
    S -- yes --> D[Destroy Job, then cleanup]
    S -- no --> A
    C -- no --> H[Held: requeue every minute]

The order inside one pass matters: the controller checks the deletion policy before the restore and the destroy decisions, right after waiting for a Job that still runs, so neither a pending restore that cannot start nor a destroy that keeps failing hides it.

Restore, then destroy

Setting captf.io/restore-state=<serial> to a serial in status.stateBackups during a hold starts a restore Job. Once the state reads again, the normal destroy follows. This is the only time a restore runs on a deleting object: with readable state the annotation is ignored and the destroy goes ahead. The restore takes the same leases as any other, is never gated, and is not retried for the same serial after a failure. See Backups and restore and the State Restore runbook.

Retain

Setting spec.deletionPolicy: Retain on the deleting object removes the finalizer without a destroy. Unlike a restore it needs no backup, and unlike stripping the finalizer by hand it loses nothing: the state Secrets that exist, the state backups and the durable and applied inputs are kept, without owner references, labeled captf.io/retained-from-uid with the object’s UID, for a later object of the same name to adopt. See Retain and Adopt.

kubectl patch <kind> <name> -n <ns> --type merge -p '{"spec":{"deletionPolicy":"Retain"}}'

It releases more than a hold on the state:

Case How the controller sees it
Held on the state StateReadable is False
The last destroy failed status.lastRun is a destroy and ApplyJobSucceeded is False (also after a restore)
The inputs Secrets are gone ApplyJobSucceeded=False/DestroyFailed with no Job, because the destroy cannot be rendered
The identity does not allow the namespace ApplyJobSucceeded=False/IdentityNotAllowed
The credentials cannot be prepared The Deleting message says the destroy waits for its credentials

The messages of all five name spec.deletionPolicy: Retain. Retain waits while a Job still holds a live run lease (five-second retries), like every other release, then emits a Normal event InfrastructureRetained counting what it kept.

Retain leaves the infrastructure running

Nothing is destroyed. Whatever the module created keeps running and costing money until an object adopts it, or you delete it through the cloud provider.