Stuck Destroy¶
CAPTF has no skip-destroy switch, only spec.deletionPolicy: Retain (see Retain instead): if a destroy Job keeps failing, the object stays with ApplyJobSucceeded=False/DestroyFailed (so Ready=False) and the controller retries with backoff forever. This applies the same way to a TerraformCluster, a TerraformMachine and a TerraformMachinePool. This page walks through recovering: back up what the destroy would remove, clean up the cloud resources another way, then remove the object’s finalizer by hand, or use Retain to keep the state.
A destroy stuck only because the registry garbage collected the pinned digest falls back on its own to the applied tag and then spec.source.image (see image pull failures). On the last image the condition reads ImagePullFailed and its message names Retain.
Removing the finalizer deletes the only record of your cloud resources
Removing the finalizer garbage-collects the state Secrets, the state backups and the durable and applied inputs Secrets through their owner references, which are the only record of the live cloud resources. Back them up, or un-own them, before you do that.
If the destroy is not failing but never starts because the state is missing or unreadable, deletion is held on purpose; see deleting while state is unreadable before removing anything by hand.
A stuck control-plane destroy blocks the control plane
A stuck destroy on a control-plane machine also blocks KubeadmControlPlane/RKE2ControlPlane remediation, scale and upgrade until it is resolved.
Retain instead¶
spec.deletionPolicy: Retain releases a deleting TerraformCluster, TerraformMachine or TerraformMachinePool without a destroy. It covers at least these four cases, which are the ones a destroy cannot get past:
- The deletion is held because the state is missing or unreadable (
StateReadableisFalse). - The last destroy Job failed (
status.lastRunis a destroy andApplyJobSucceededisFalse). - The destroy cannot be rendered because the inputs Secrets (durable and applied) are gone (
ApplyJobSucceededisFalse/DestroyFailed). - The module image cannot be pulled at all: the condition reads
ApplyJobSucceeded=False/ImagePullFailedand no other image is left. - The destroy cannot start because the identity no longer allows the namespace or was deleted (
IdentityNotAllowed), or the runner credentials cannot be prepared (theDeletingcondition says the destroy waits for its credentials).
Patch the field on the deleting object; updates are allowed while it is being deleted:
The controller waits for any running Job, then removes the finalizer without a destroy and emits an InfrastructureRetained event. It keeps the state Secrets, the state backups and the durable and applied inputs Secrets, removes their owner references so they are not garbage-collected, and labels each captf.io/retained-from-uid=<uid>. Whatever the module created keeps running, with nothing managing it, so you either clean the cloud resources up yourself (step 4 below) or adopt them later: a new object of the same kind, namespace and name with spec.adoptRetainedState: true takes the retained state over. See Retain and Adopt. Because the state and backups survive, backing them up first (step 2) is not required for Retain. Retain is decided before restore and destroy, so it also works while a restore is pending.
Retain does not wait for a destroy to fail: with Retain set, even a deletion whose destroy could succeed runs no destroy. For machines created from an immutable TerraformMachineTemplate, set the policy on the TerraformCluster (spec.defaults.deletionPolicy or spec.deletionPolicy) or patch the TerraformMachine itself, because changing the template rolls the machines. To discard the retained state instead, see discarding retained state. For more on a held delete, see deleting while state is unreadable.
Terminating namespaces¶
A deleting object needs credentials only when it runs a Job. With the namespace terminating, an object that never applied finishes at once, and a held one can be retained. A destroy or restore waiting on credentials shows the Deleting condition message The <op> Job waits for its credentials: … and retries. One whose credentials are ready still cannot create its run Lease or Job there: Deleting says Namespace <ns> is terminating, so the destroy Job cannot be created, and the object is retried every 30 seconds without an error. The namespace’s deletion removes the state Secrets too, so set spec.deletionPolicy: Retain (after copying the state out) or stop the namespace deletion if the destroy must run.
Before you begin
get,patchanddeleteaccess to Secrets and to the stuck object, in its namespace, on the management cluster.- Replace
<ns>,<name>and<kind>below with the object’s namespace, name and Kind (terraformcluster,terraformmachineorterraformmachinepool; the lowercase singular works withkubectl). Run every command against the management cluster.
1. Find the state suffix¶
State Secret and backup Secret names are keyed by a suffix the controller derives from the object’s namespace, kind and name. Read it back from the object’s own status rather than recomputing it:
Save the output as <suffix> for the commands below.
2. Back up the state and inputs Secrets¶
kubectl get secret -n <ns> -l tfstate=true,tfstateSecretSuffix=<suffix> -o yaml > backup-state.yaml
kubectl get secret -n <ns> -l captf.io/state-backup=true,captf.io/state-backup-suffix=<suffix> -o yaml > backup-state-backups.yaml
kubectl get secret -n <ns> captf-inputs-<kindshort>-<name> -o yaml > backup-inputs.yaml
kubectl get secret -n <ns> captf-applied-<kindshort>-<name> -o yaml > backup-applied.yaml
<kindshort> is c for a TerraformCluster, m for a TerraformMachine, mp for a TerraformMachinePool. The first selector matches the base state Secret and every -part-N chunk of a large Terraform state in one call; the second matches every backup Secret (and its chunks) taken of this object — see Terraform State and the state restore runbook. Both kinds of Secret are owned by the object and are garbage-collected along with it once its finalizer is removed.
backup-applied.yaml’s captf.io/image-digest annotation (see Annotations, labels and finalizers) names the exact image that ran the last successful apply — you will need it in step 4. If the newest attempt failed after it may have applied (the durable Secret carries captf.io/may-have-applied), the destroy renders that attempt’s files instead: take them from backup-inputs.yaml.
3. Un-own the Secrets, if you want them to survive finalizer removal¶
kubectl patch takes names, not a label selector, so patch each Secret by name:
for s in $(kubectl get secret -n <ns> -l tfstate=true,tfstateSecretSuffix=<suffix> -o name); do
kubectl patch -n <ns> "$s" --type=json -p '[{"op":"remove","path":"/metadata/ownerReferences"}]'
done
for s in $(kubectl get secret -n <ns> -l captf.io/state-backup=true,captf.io/state-backup-suffix=<suffix> -o name); do
kubectl patch -n <ns> "$s" --type=json -p '[{"op":"remove","path":"/metadata/ownerReferences"}]'
done
kubectl patch secret -n <ns> captf-inputs-<kindshort>-<name> \
--type=json -p '[{"op":"remove","path":"/metadata/ownerReferences"}]'
Skip this if you would rather rely on step 2’s backup-*.yaml files: with those in hand you don’t need the live Secrets to survive finalizer removal.
4. Clean up the cloud resources¶
Either clean up out of band (the cloud console, or the module’s own tooling), or run the pinned image’s terraform/tofu binary by hand against the backed-up state and inputs. The image’s own binary lives at /captf/runtime (Image Contract); the Job’s own use of it, including its exact -backend-config flags, is in Job Environment.
docker run --rm --entrypoint /captf/runtime \
-v "$PWD/root:/captf/work/root" -w /captf/work/root \
<image>@<digest> init -input=false -no-color \
-backend-config=secret_suffix=<suffix> -backend-config=namespace=<ns> \
-backend-config=in_cluster_config=true -backend-config=labels=<labels>
docker run --rm --entrypoint /captf/runtime \
-v "$PWD/root:/captf/work/root" -w /captf/work/root \
<image>@<digest> destroy -auto-approve -input=false -no-color \
-var-file=terraform.tfvars.json
<image>@<digest>: the digest may have been garbage collected (checkcaptf.io/unpullable-imageson the durable Secret). Then use thecaptf.io/imagetag, or the spec image, instead. It is the repository frombackup-applied.yaml’scaptf.io/imageannotation (drop any:tag) plus@and the digest from itscaptf.io/image-digestannotation.<labels>must be the same HCL object the Job itself would pass, orinitreads an empty state: the backend selects state by this whole map. Copy it frombackup-state.yaml’s base Secret.metadata.labels, droppingtfstate,tfstateSecretSuffixandtfstateWorkspace(the backend sets those itself), as{"key"="value",...}; see Annotations, labels and finalizers for what each remaining key means../rootneedsmain.tf.jsonandterraform.tfvars.jsonfrombackup-applied.yaml’s data (the applied Secret’s rendered root module and tfvars; usebackup-inputs.yaml’s when the destroy would render the attempt record), and the identity’s credentials as environment variables (-e <KEY>=<value>for each key of the mirrored credentials Secret, or a file for a file-based one — see Identities and credentials).-backend-config=in_cluster_config=truereads the pod’s own ServiceAccount token, so this only works run from inside the cluster (for example, a debug pod using thecaptf-runnerServiceAccount). Running it from a workstation needs a kubeconfig-based backend configuration instead.- Restoring
backup-state.yamlfirst (recreate the Secrets it holds) and letting thekubernetesbackend read that live state also works, and skips reconstructing the backend configuration by hand.
5. Remove the finalizer¶
Stripping the finalizer skips the controller
No destroy, no cleanup and no event run. Finish steps 2 and 4 first: the cloud resources the object created keep running, untracked, and the state backups go with the object.
kubectl patch <kind> -n <ns> <name> --type=json \
-p '[{"op":"remove","path":"/metadata/finalizers"}]'
Use the lowercase, plural CRD resource name (for example, terraformmachines) if your kubectl version needs it instead of the Kind. This removes the whole metadata.finalizers array: safe for a Terraform* object, which carries only CAPTF’s own finalizer, but check .metadata.finalizers first if something else may have added one, and remove that entry by index instead.
Confirm it worked¶
kubectl get <kind> -n <ns> <name>
kubectl get secret -n <ns> -l tfstate=true,tfstateSecretSuffix=<suffix>
The first command reports NotFound once the object is gone. The second shows nothing unless you un-owned the Secrets in step 3, in which case they are exactly what you chose to keep.
The inputs Secrets are missing¶
If the object reports ApplyJobSucceeded=False/DestroyFailed with the message “The inputs Secrets (durable and applied) are missing, so destroy cannot be rendered”, both inputs records are gone or were never written: the durable Secret (captf-inputs-<kindshort>-<name>, the newest attempt) and the applied Secret (captf-applied-<kindshort>-<name>: the rendered root module, tfvars and pinned image from the last successful apply — see Job Inputs). One surviving record is enough: destroy renders whichever describes the state (see Which record a destroy renders).
A destroy that renders inputs which differ from the state’s emits the Warning DestroyInputsMismatch, naming the Job and both hashes. If it names an attempt that failed partway, the destroy is using that attempt’s own inputs, as intended. If a typo in the provider config made an apply fail in validate or plan, that typo was never applied, so reverting the spec fixes a stuck destroy: the destroy keeps rendering what actually applied.
TerraformMachine is the persistent case: it is immutable and never falls back to re-rendering current inputs for a destroy, since the Machine and its bootstrap Secret a rebuild would need are usually already gone by the time destroy runs, so once both of its Secrets are gone the condition never clears on its own. TerraformCluster and TerraformMachinePool (both mutable) fall back to building current inputs instead; they show the same message only while that build is gated (for example, waiting on a dependency that is itself being deleted), and it usually clears once the gate does. The controller retries forever either way; it never invents inputs to destroy with.
To recover:
- If you have a backup (
backup-inputs.yamlandbackup-applied.yamlfrom a previous run of step 2 above, or any earlier copy of either Secret), recreate it withkubectl apply -f backup-applied.yamlafter removingmetadata.uidandmetadata.resourceVersionfrom the YAML: reapplying the exact object restores its owner reference, labels and annotations, including the pinnedcaptf.io/imageandcaptf.io/image-digest(which live on the applied Secret). The next reconcile reads it and starts the destroy Job. - If you have no backup, the controller cannot destroy the object’s resources. Back up the state (step 2), then clean up out of band (step 4): without the rendered
main.tf.jsonandterraform.tfvars.json, running the module by hand needs reconstructing them, but the state lists every resource the module created. Instead of removing the finalizer by hand, Retain releases the object without a destroy and records the cause.status.source.imageandstatus.source.imageDigeststill name the image the last Job ran. Then remove the finalizer (step 5). Removing the finalizer without cleaning up the cloud resources first leaves them running: they stay in the cloud, unmanaged, with nothing in Kubernetes recording that they ever existed.