Skip to content

Deletion and Teardown

Deleting a TerraformCluster, TerraformMachine or TerraformMachinePool means running Terraform’s destroy in a Job, then removing what CAPTF stored about the object. Most of the time nothing needs doing: you delete the Cluster Cluster API owns and everything follows. This chapter explains the order things happen in, what holds a deletion, how a deletion ends, and what is left behind when you short-circuit it. The step-by-step recoveries stay in the runbooks; this chapter is the model behind them.

The pages:

  1. This page: the rules in one table, and the pages that follow.
  2. Order and finalizers: who deletes what first, and when the finalizer comes off.
  3. The destroy Job: what runs, what does not gate it, and how it fails.
  4. Held deletions: missing or unreadable state, restore, and Retain.
  5. Retain and Adopt: delete without a destroy, keep the state, and adopt it later.
  6. Terminating namespaces: deletions that need no Job, and the Secrets that go with the namespace.
  7. Cleanup and garbage collection: what the controller deletes, what Kubernetes deletes, and the RBAC sweep.
  8. clusterctl move: the delete that skips the delete path.
  9. Stripping a finalizer by hand: the consequences.
  10. My object will not delete: a flowchart from the symptom to the action.

In this section

The rules

A deleting object follows these rules, in this order. The first that applies decides the pass.

Situation What happens Page
The object or its Cluster is paused Nothing: a paused object runs only Job bookkeeping, so it neither destroys nor releases Order
A Job of the object is running The deletion waits for it; the destroy starts after Order
A TerraformCluster with machines or pools left DeletionBlocked=True/DependentsExist, no destroy yet Order
No state, and the object never applied The finalizer comes off at once Held
No state, and the object applied before Held: StateReadable=False/StateLost Held
No state, and an apply may have created resources without saving any Held: StateReadable=False/ApplyOutcomeUnknown until captf.io/confirm-no-resources Held
State exists but cannot be read Held: StateCorrupt, StateEncrypted or StateInconsistent Held
spec.deletionPolicy: Retain No destroy: the state, backups and inputs Secrets are kept, and the finalizer comes off Retain
Readable state A destroy Job runs; no gate or approval applies Destroy
The destroy succeeded Cleanup runs and the finalizer comes off Cleanup
The destroy failed or cannot start Retried with backoff, forever; Retain releases it Held

Foreground deletion

An object’s state Secrets, state backups and inputs Secrets carry an owner reference to it, so they go with the object. The object’s finalizer keeps the object until its destroy is done, but not those dependents: with propagationPolicy: Foreground (kubectl delete --cascade=foreground, Argo CD’s default prune, a Cluster deleted that way), the garbage collector deletes every dependent at once. Two things keep that from losing the state:

  • The Secrets CAPTF cannot rebuild (the base state Secret, every backup chunk, both inputs records) carry the finalizer captf.io/state-protection. A cascading delete only marks them, and their data stays readable until CAPTF’s own cleanup removes them.
  • On its first pass, CAPTF removes the object’s foregroundDeletion finalizer (ForegroundDeletionConverted), turning the deletion into a background one: the dependents then go only after the object, after the destroy.

What the collector deleted before that pass is lost if it was not protected: a running Job, and the -part-N chunks of a large state, which the backend deletes itself when a state shrinks and so cannot carry the finalizer. A state left without them reads as inconsistent and holds the deletion for a restore from a backup.

What the controller never does

  • It never drops the finalizer while a state Secret may still describe live resources, except on an explicit Retain, which keeps the state.
  • It never destroys against a state it cannot read.
  • It never invents inputs to destroy with: the destroy renders from the inputs record that describes the state, the durable or the applied Secret (or, for a cluster or pool with neither, the current inputs when they build). See Which record a destroy renders.
  • It never skips the destroy because it failed. There is no skip-destroy annotation, only Retain.