Skip to content

nf destroy

Preview and remove managed infrastructure using the app and backend that applied it. For Terraform/OpenTofu compatibility, pass a synthesized stack directory to --stack.

Synopsis

nf destroy [OPTIONS] [TERRAFORM_ARGS]...

Main Modes

App-Scoped Direct Destroy

nf destroy --app demo

Configured App Stack Direct Destroy

nf destroy --app platform --stack network

Legacy Stack Destroy

nf destroy --stack .nf.out/platform/network

Options

  • --app TEXT: app selector for direct execution
  • --stack, -s TEXT: configured stack selector or synthesized stack directory
  • --legacy-bin TEXT: path to the legacy CLI binary
  • --outdir PATH: synthesized output directory override
  • --auto-approve: skip interactive approval
  • --recovery-strategy checkpoint|rollback|none: what a failed direct destroy does (default checkpoint)
  • --run-status/--no-run-status: keep the run's live status beside its state for nf follow (default on)

JSON output

--json (with --auto-approve) prints the destroy as one JSON document on stdout, in the format of nf apply's with "operation": "destroy"; everything else nf prints goes to stderr.

Important Behavior

  • direct destroy supports whole apps and individual stacks within configured apps
  • the whole app's direct destroy also destroys what state holds of stacks the app no longer declares (their resources, data source results and outputs); a stack's destroy covers only that stack
  • a resource with lifecycle.prevent_destroy stops the destroy before approval, nothing deleted, as in Terraform. The rule in the resource's code applies, so removing it there allows the destroy without an apply first; a resource the app no longer declares is held to the rule its state entry recorded
  • a resource with lifecycle.retain is kept, its object and its state entry: the preview, the approval, the result and --json list it with its reason, and the destroy succeeds when it deletes everything else and verifies what it keeps (see Keeping resources)
  • a failed direct destroy is recovered as --recovery-strategy (or the app's recovery_strategy) says: checkpoint saves the state before the destroy and reports where; rollback has nothing to delete (a destroy creates nothing) and reports the deletions it cannot revert; none does neither
  • legacy destroy uses the synthesized stack directory
  • if you need raw Terraform/OpenTofu arguments or --legacy-bin, use the legacy --stack path instead of --app
  • explicit filesystem stack paths and synthesized stack selectors stay on the legacy subprocess path
  • stack directories that only contain legacy phased Terraform artifacts are rejected on the --stack path; use --app direct execution instead
  • use extra caution with ambiguous stack names and shared output trees

Keeping resources

Some resources outlive a teardown on purpose: a bucket of customer data, a backup vault. Give them retain with the reason, and the resources that protect them too; nf destroy keeps them and says why, instead of failing on them as prevent_destroy makes it fail:

# coding: nf_autoid

from nf.core import App, Bind, BindContext, LifecycleRules, Stack
from nf.core.runtime import RuntimeCheckContext, retained_check
from nf.providers.hashicorp.aws import (
    AwsProvider,
    S3Bucket,
    S3BucketNotification,
    S3BucketPublicAccessBlock,
    S3BucketServerSideEncryptionConfiguration,
)


@retained_check("the bucket's objects stay encrypted with KMS")
def encrypted(context: RuntimeCheckContext) -> bool:
    rules = (context.current_state or {}).get("rule") or []
    return any(rule["apply_server_side_encryption_by_default"][0]["sse_algorithm"] == "aws:kms" for rule in rules)


with App(nf_id="data") as app, Stack() as storage, BindContext:
    aws: Bind = AwsProvider(region="eu-west-1")
    data: Bind = S3Bucket(bucket="acme-data", lifecycle=LifecycleRules(retain="customer data outlives the deployment"))
    encryption: Bind = S3BucketServerSideEncryptionConfiguration(
        bucket=data.output.id,
        rule=[{"apply_server_side_encryption_by_default": [{"sse_algorithm": "aws:kms"}]}],
        lifecycle=LifecycleRules(retain="keeps the retained bucket encrypted"),
        runtime_checks=[encrypted()],
    )
    private: Bind = S3BucketPublicAccessBlock(
        bucket=data.output.id,
        block_public_acls=True,
        block_public_policy=True,
        ignore_public_acls=True,
        restrict_public_buckets=True,
        lifecycle=LifecycleRules(retain="keeps the retained bucket private"),
    )
    # Goes with the deployment, though it depends on the retained bucket
    events: Bind = S3BucketNotification(bucket=data.output.id, eventbridge=True, lifecycle=LifecycleRules(retain=False))
  • What is kept: the object and its state entry, never forgotten. The next apply finds it in state as before; the next destroy keeps it again.
  • Its protection: what a retained resource depends on must be retained too, and so must what depends on it (its policy, encryption, public access block, versioning, lifecycle configuration), unless that says retain=False. A destroy that would delete either is refused before approval, naming the resource, nothing deleted.
  • Verification: once the deletions are done, the destroy reads each object it keeps from its provider and runs its retained runtime checks against what it read. An object that is gone, cannot be read (a denied read, a provider that cannot be configured), or fails a check (including one that reads a value that is not known) fails the destroy, which says which and why. The checks are what establish the protection the app needs; verification covers preservation, not recoverability.
  • Removed from the app: the rule is recorded with the state entry, so a retained resource removed from the code is still kept: an apply keeps it rather than delete it (its plan says so), and a destroy keeps it and runs the checks its entry recorded. To let it go, take retain out of its code (a destroy then deletes it, as for prevent_destroy), or remove its entry with nf state rm.
  • Replacements: retain is about teardown. A change that replaces the resource replaces it; prevent_destroy is what refuses that.

--json lists what a destroy kept under "retained", each with its address and reason.

Interrupting

Ctrl-C works as it does for Terraform:

  • before anything changes (planning, the approval prompt) it aborts the command
  • once a direct destroy changes infrastructure, the first Ctrl-C stops it cleanly: resources being applied finish and are recorded in state, nothing else starts, and the command exits 1 saying it stopped. The app's recovery_strategy applies as for any failed destroy (see Stop an apply in progress)
  • a second Ctrl-C stops without waiting: changes in flight may not be recorded in state, so check them before the next apply (nf state recover for graph state). A third exits at once
  • on the legacy --stack path, Terraform/OpenTofu handles Ctrl-C itself (graceful, then immediate) and nf waits for it; a third Ctrl-C kills it