Skip to main content

Synopsis

Destroy previously-created infrastructure

Description

The terraform destroy command is a convenient way to destroy all remote objects managed by a particular Terraform configuration. This command is a convenience alias for:
terraform apply -destroy
While you will typically not want to destroy long-lived objects in a production environment, Terraform is sometimes used to manage ephemeral infrastructure for development purposes, in which case you can use terraform destroy to conveniently clean up all of those temporary objects once you are finished with them.

Usage

terraform destroy [options]

Options

Approval Options

-auto-approve
boolean
default:"false"
Skip interactive approval of the destroy plan before destroying.
-input
boolean
default:"true"
Ask for input for variables if not directly set.

Targeting Options

-target
string
Limit the destroy operation to only the given module, resource, or resource instance and all of its dependencies. You can use this option multiple times to include more than one object. This is for exceptional use only.

Variable Options

-var
string
Set a value for one of the input variables in the root module of the configuration. Use this option more than once to set more than one variable. Format: -var 'name=value'
-var-file
string
Load variable values from the given file, in addition to the default files terraform.tfvars and *.auto.tfvars. Use this option more than once to include more than one variables file.

State Management

-lock
boolean
default:"true"
Don’t hold a state lock during the operation. This is dangerous if others might concurrently run commands against the same workspace.
-lock-timeout
duration
default:"0s"
Duration to retry a state lock. For example: ”10s” for 10 seconds.
-state
string
Path to read and save state (unless state-out is specified). Defaults to “terraform.tfstate”. Legacy option for the local backend only.
-state-out
string
Path to write state to that is different than “-state”. This can be used to preserve the old state. Legacy option for the local backend only.
-backup
string
Path to backup the existing state file before modifying. Defaults to the “-state-out” path with “.backup” extension. Set to ”-” to disable backup.

Performance Options

-parallelism
number
default:"10"
Limit the number of parallel resource operations.
-refresh
boolean
default:"true"
Update the state prior to checking for differences. Set to false to skip refreshing.

Output Options

-compact-warnings
boolean
default:"false"
If Terraform produces any warnings that are not accompanied by errors, show them in a more compact form that includes only the summary messages.
-json
boolean
default:"false"
Produce output in a machine-readable JSON format. When using this flag, you must also specify -auto-approve.
-no-color
boolean
default:"false"
If specified, output won’t contain any color.

Examples

Basic Destroy

Destroy all resources with interactive approval:
$ terraform destroy

Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # aws_instance.example will be destroyed
  - resource "aws_instance" "example" {
      - ami                          = "ami-0c55b159cbfafe1f0" -> null
      - instance_type                = "t2.micro" -> null
      - id                           = "i-1234567890abcdef0" -> null
      ...
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_instance.example: Destroying... [id=i-1234567890abcdef0]
aws_instance.example: Destruction complete after 30s

Destroy complete! Resources: 1 destroyed.

Auto-Approve Destroy

Destroy all resources without interactive approval:
$ terraform destroy -auto-approve

aws_instance.example: Destroying... [id=i-1234567890abcdef0]
aws_instance.example: Destruction complete after 30s

Destroy complete! Resources: 1 destroyed.

Destroy Specific Resources

Destroy only specific resources:
$ terraform destroy -target=aws_instance.example

Terraform will perform the following actions:

  # aws_instance.example will be destroyed
  - resource "aws_instance" "example" {
      ...
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_instance.example: Destroying... [id=i-1234567890abcdef0]
aws_instance.example: Destruction complete after 30s

Destroy complete! Resources: 1 destroyed.

Destroy with Variables

Destroy with variable values:
$ terraform destroy -var="region=us-west-2" -var-file="prod.tfvars" -auto-approve

Multiple Targets

Destroy multiple specific resources:
$ terraform destroy \
  -target=aws_instance.web \
  -target=aws_security_group.web \
  -auto-approve

aws_instance.web: Destroying...
aws_instance.web: Destruction complete after 30s
aws_security_group.web: Destroying...
aws_security_group.web: Destruction complete after 5s

Destroy complete! Resources: 2 destroyed.

Exit Codes

  • 0 - Success
  • 1 - Error occurred

Important Notes

The terraform destroy command is irreversible. Once resources are destroyed, they cannot be recovered unless you have backups or snapshots managed outside of Terraform.
This command also accepts many of the plan-customization options accepted by the terraform plan command. For more information on those options, run:
terraform plan -help

Build docs developers (and LLMs) love