Terraform Tutorial — terraform destroy
terraform destroy removes all resources managed by the current Terraform configuration. It’s the reverse of terraform apply.
Running destroy
$ terraform destroy
Terraform will perform the following actions:
# aws_instance.web will be destroyed
- resource "aws_instance" "web" {
- ami = "ami-0c55b159cbfafe1f0" -> null
- instance_type = "t3.micro" -> null
- id = "i-0abc123def456" -> null
}
# aws_s3_bucket.main will be destroyed
- resource "aws_s3_bucket" "main" {
- bucket = "my-app-bucket" -> null
}
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources?
Enter a value: yes
aws_instance.web: Destroying... [id=i-0abc123def456]
aws_instance.web: Destruction complete after 45s
aws_s3_bucket.main: Destroying... [id=my-app-bucket]
aws_s3_bucket.main: Destruction complete after 2s
Destroy complete! Resources: 2 destroyed.
Destroy with Auto-Approve
$ terraform destroy -auto-approve
⚠️ Warning: This immediately destroys all resources without confirmation. Use only in dev/test environments or automated cleanup pipelines.
Targeted Destroy
Destroy a specific resource without touching others:
$ terraform destroy -target=aws_instance.web
Destroy Order
Terraform destroys resources in the reverse order of their dependencies:
Create order: VPC → Subnet → EC2 Instance
Destroy order: EC2 Instance → Subnet → VPC
Alternative: Remove from Config
Instead of destroy, you can remove the resource from your .tf files and run apply:
# 1. Delete the resource block from main.tf
# 2. Apply
$ terraform apply
# Result: Terraform destroys the removed resource
Prevent Accidental Destruction
Use prevent_destroy lifecycle:
resource "aws_s3_bucket" "critical_data" {
bucket = "production-database-backups"
lifecycle {
prevent_destroy = true
}
}
If someone runs terraform destroy, it fails:
Error: Instance cannot be destroyed
Resource aws_s3_bucket.critical_data has lifecycle.prevent_destroy
set, but the plan calls for this resource to be destroyed.
What’s Next?
Let’s explore useful flags and tips for the complete workflow.