terraform destroy — the Terraform command that deletes Terraform-managed infrastructure from your current working directory (or workspace).
Terraform destroy terminates resources tracked in your current state and shows a destruction plan before making changes. Use it to clean up environments you no longer need.
Overview
terraform destroyremoves all Terraform-managed resources in the active workspace by default.- You can also remove resources by editing configuration files (deleting resource blocks) and running
terraform apply, which will detect and destroy resources that no longer appear in the configuration. - Targeted destruction (
-target) lets you destroy specific resources but is an advanced operation and can lead to unexpected results if configuration files are not updated accordingly.
Ways to remove resources
Full-destroy (default)
Run this to remove everything Terraform manages in the current workspace:yes to proceed.
Remove by changing configuration (recommended for selective deletes)
Delete the resource blocks from your.tf files, then run:
State file behavior
When Terraform destroys resources (either viaterraform destroy or by removing blocks and applying), it also removes those entries from the state file. Once removed, Terraform no longer tracks those resources.
This behavior ensures the state accurately reflects the resources currently managed. If a resource remains defined in the configuration but is absent from the state, Terraform will attempt to recreate it on the next terraform apply.
Destroy operations are effectively irreversible from Terraform’s perspective. There is no built-in “undo” or recycle bin. Some cloud providers may offer snapshots or backups, but those recovery paths are outside Terraform. Always review the destruction plan carefully before typing
yes, and avoid running destroy accidentally in production.
How destroy respects dependencies
Terraform destroys resources while respecting the dependency graph in reverse order of creation. This prevents errors such as deleting a network before deleting VMs that depend on it. Example ordering for a network topology:- VMs (depend on subnets) are destroyed first.
- Subnets (depend on network) are destroyed next.
- Network is destroyed last.
terraform destroy or a config-driven terraform apply.
Targeted destroy
To destroy a specific resource (or set of resources), use the-target flag:
aws_instance.web_one and leave other managed resources intact.
Tip: If you want to preview changes before destroying a specific resource, run
terraform plan -destroy -target=... to see the destruction plan without applying it.Caution about targeted destroys
Using-target is an advanced operation. If you destroy a resource with -target but leave its block in your configuration, the next terraform apply will attempt to recreate it because it still exists in code but not in state.
Example sequence that will recreate a resource if its configuration remains:
terraform apply.
Best practices
- Prefer removing resource blocks and running
terraform applyfor selective deletions—this keeps configuration and state aligned. - Use workspaces to separate environments (dev/stage/prod) to reduce risk of accidental destruction.
- Avoid
terraform destroyin production unless you are certain; consider automated safeguards or manual approvals. - Use
terraform plan -destroyto preview what will be removed. - Never run
terraform destroywith-auto-approvein production without a prior review.
Quick summary
terraform destroyremoves Terraform-managed resources and prompts for confirmation after showing a destruction plan.- Removing resource blocks and running
terraform applyis the recommended way to selectively delete resources. - Destroy operations also remove entries from the state file.
- Destruction is irreversible via Terraform; always inspect the plan.
- Use
-targetonly when necessary and remove config blocks if you don’t want resources recreated.