Core Terraform Workflow · Topic 3 of 3

Destroy and Format

Terraform Associate (004): destroy managed infrastructure with terraform destroy or plan -destroy, and keep code in canonical style with terraform fmt.

  • 4 min
In a nutshell
  • terraform destroy equals terraform apply -destroy; it shows a plan and asks for yes unless -auto-approve is set.
  • terraform plan -destroy previews destruction without changing anything.
  • Destruction follows reverse dependency order and touches only objects in state.

What it is

terraform destroy removes every object that the current working directory and workspace manage, according to state. It is the end of a configuration's life cycle, common for short-lived test and development environments.

terraform fmt rewrites configuration files into Terraform's canonical format and style. It changes layout only, never meaning, and is usually run before every commit or checked in a pipeline.

Managed objectslisted in stateterraform destroyDestroy planyesObjects removedState emptied

How it works

Destroying everything

terraform plan -destroy
terraform destroy

terraform destroy is a shortcut for terraform apply -destroy. It builds a plan in which every managed object is marked -, shows it and waits for yes; -auto-approve skips the prompt.

terraform plan -destroy previews the same plan without changing anything. You can save it with -out=FILE and apply that file later.

Terraform destroys in reverse dependency order: an instance goes before the subnet it sits in, and the subnet before its VPC. Objects that Terraform does not manage are never touched, even if they sit in the same account.

After a destroy

The configuration files are not touched, and the state file remains with no resources in it. Running terraform apply again builds the whole environment from scratch, which is why destroy suits temporary environments.

Destroying part of the infrastructure

GoalHow
Remove one resource for goodDelete its block from the configuration, then plan and apply
Remove one resource in an emergencyterraform destroy -target=ADDRESS (exceptional use only)
Stop managing a resource but keep itA removed block with destroy = false in its lifecycle block
Protect a resource from deletionprevent_destroy = true in its lifecycle block

Deleting a block is the normal way to remove one object. The next plan shows it with -, because it is in state but no longer in the configuration.

resource "aws_db_instance" "main" {
  # ... other arguments ...

  lifecycle {
    prevent_destroy = true
  }
}

With prevent_destroy, any plan that would destroy this object, including terraform destroy, fails with an error. It does not help if you delete the whole resource block, because the setting goes with it.

Formatting with terraform fmt

terraform fmt
terraform fmt -recursive
terraform fmt -check

By default fmt rewrites .tf and .tfvars files in the current directory only and lists the files it changed. -recursive also processes subdirectories, such as local modules.

OptionEffect
-checkChange nothing; exit with a non-zero status if any file needs formatting
-diffShow the formatting changes as a diff
-recursiveInclude subdirectories
-write=falseDo not rewrite files

-check is the usual choice in a pipeline, where a non-zero exit fails the build.

What fmt changes

resource "aws_instance" "web" {
  ami           = "ami-0123456789abcdef0"
  instance_type = "t3.micro"

  tags = {
    Name = "web"
  }
}

The output above is canonical: two spaces per nesting level and the equals signs of consecutive arguments aligned. fmt fixes indentation, alignment and spacing like this, but it keeps your blank lines rather than adding them.

fmt does not rename resources, reorder blocks, catch wrong values or fix logic. A file with a syntax error cannot be parsed, so fmt reports the error instead of formatting it.

Style conventions beyond fmt

HashiCorp's style guide adds habits that fmt does not enforce: lowercase names with underscores, meta-arguments such as count or for_each first in a block, and nested blocks after arguments. Common file names are main.tf, variables.tf, outputs.tf and providers.tf.

How the exam asks about this

  • "Preview what terraform destroy would remove without removing anything." Run terraform plan -destroy.
  • "A pipeline must fail when configuration is not formatted, without rewriting files." Use terraform fmt -check, with -recursive to include subdirectories.
  • "True or false: terraform destroy is equivalent to terraform apply -destroy." True.

What to remember for the exam

  • terraform destroy equals terraform apply -destroy; it shows a plan and asks for yes unless -auto-approve is set.
  • terraform plan -destroy previews destruction without changing anything.
  • Destruction follows reverse dependency order and touches only objects in state.
  • Removing a resource block and applying destroys that one object.
  • terraform fmt rewrites files to canonical style in the current directory; -recursive adds subdirectories; -check only reports.
  • fmt changes layout, never behavior, and cannot fix syntax or logic errors.
Watch out

prevent_destroy = true blocks destroy plans only while the resource block exists. Deleting the block removes the protection along with it.

Common mistakes

Thinking destroy removes everything in the account. Terraform destroys only the objects recorded in the current state. Manually created resources are untouched.

Expecting fmt to validate configuration. fmt only adjusts layout and style. Use terraform validate to check arguments, types and references.

Forgetting -recursive. Plain terraform fmt skips subdirectories, so local modules stay unformatted unless you add -recursive.

Check yourself

  • What is the difference between terraform destroy and terraform plan -destroy?

    terraform destroy builds a destroy plan and, after approval, removes every managed object; terraform plan -destroy only shows that plan and changes nothing.

  • How do you make a CI pipeline fail on unformatted files without changing them?

    Run terraform fmt -check, adding -recursive for subdirectories; it exits with a non-zero status when any file needs formatting.

  • What happens to a resource if you delete its block from the configuration and apply?

    Terraform sees the object in state but not in the configuration, plans to destroy it, and removes it after approval.

Topic 3 of 3 in Core Terraform Workflow

Test yourself on Core Terraform Workflow

The full HashiCorp Terraform Associate 004 question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.