Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
๐ Terraform Destroy: Safely Removing Managed Infrastructure
In Terraform, every lifecycle has two essential stages:
- Creating/Updating Infrastructure โ (
terraform apply
) - Deleting Infrastructure โ (
terraform destroy
)
While terraform apply
is like constructing a building, terraform destroy
is like safely dismantling it without leaving debris.
Terraform Destroy is the command used to delete all or specific infrastructure resources that Terraform manages. It ensures that your cloud environment does not accumulate unused or costly resources.
๐ Without terraform destroy
, cloud bills can skyrocket, resources can pile up, and environments may become messy.
โ๏ธ What Does Terraform Destroy Do?
When you run:
terraform destroy
Terraform will:
-
Read Configuration (
.tf
files) โ Identifies what infrastructure is defined. -
Load State (
terraform.tfstate
) โ Knows whatโs currently deployed. -
Generate Execution Plan โ Lists resources that will be destroyed.
-
Prompt for Confirmation โ Asks you before deletion:
Do you really want to destroy all resources?Only 'yes' will be accepted to confirm. -
Destroy Resources โ Safely deletes infrastructure.
-
Update State File โ Removes destroyed resources from state.
๐ Terraform Destroy Syntax & Options
- Basic Destroy
terraform destroy
- Skip Confirmation Prompt
terraform destroy -auto-approve
- Destroy Specific Resource
terraform destroy -target=aws_s3_bucket.example
- Use Variable File During Destroy
terraform destroy -var-file="prod.tfvars"
๐ 3 Unique Real-World Examples of Terraform Destroy
โ Example 1: Destroying an AWS S3 Bucket
main.tf
provider "aws" { region = "us-east-1"}
resource "aws_s3_bucket" "example" { bucket = "terraform-destroy-example-bucket" acl = "private"}
Commands:
terraform initterraform applyterraform destroy
Expected Output:
aws_s3_bucket.example: Destroying...aws_s3_bucket.example: Destruction complete after 2sDestroy complete! Resources: 1 destroyed.
๐ This deletes the bucket completely from AWS.
โ Example 2: Destroying a Specific Resource Only
Imagine you deployed two resources:
resource "aws_s3_bucket" "bucket1" { bucket = "bucket-1" acl = "private"}
resource "aws_s3_bucket" "bucket2" { bucket = "bucket-2" acl = "private"}
Destroy only bucket1
:
terraform destroy -target=aws_s3_bucket.bucket1
Output:
aws_s3_bucket.bucket1: Destroying...Destroy complete! Resources: 1 destroyed.
๐ bucket2
remains untouched.
โ Example 3: Destroying Azure Virtual Machine
main.tf
provider "azurerm" { features {}}
resource "azurerm_resource_group" "rg" { name = "terraform-rg" location = "East US"}
resource "azurerm_virtual_network" "vnet" { name = "terraform-vnet" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name address_space = ["10.0.0.0/16"]}
resource "azurerm_subnet" "subnet" { name = "terraform-subnet" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.vnet.name address_prefixes = ["10.0.1.0/24"]}
resource "azurerm_network_interface" "nic" { name = "terraform-nic" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name
ip_configuration { name = "internal" subnet_id = azurerm_subnet.subnet.id private_ip_address_allocation = "Dynamic" }}
resource "azurerm_linux_virtual_machine" "vm" { name = "terraform-vm" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location size = "Standard_B1s" admin_username = "azureuser" network_interface_ids = [azurerm_network_interface.nic.id]
admin_ssh_key { username = "azureuser" public_key = file("~/.ssh/id_rsa.pub") }}
Destroy Command:
terraform destroy
Output:
azurerm_linux_virtual_machine.vm: Destroying...azurerm_network_interface.nic: Destroying...azurerm_virtual_network.vnet: Destroying...Destroy complete! Resources: 5 destroyed.
๐ Entire VM environment is removed.
๐ฏ Why is Terraform Destroy Important?
-
Cost Management
- Prevents unused resources from generating cloud bills.
-
Environment Cleanups
- Temporary dev/test environments can be safely removed.
-
Disaster Recovery Testing
- Simulate infrastructure teardown and re-deploy.
-
Avoid Manual Cleanup
- Eliminates risk of forgetting resources.
-
Security
- Removes unused, potentially vulnerable resources.
๐ง How to Remember Terraform Destroy (Exam & Interview)
Mnemonic: D.E.L.E.T.E
- D โ Destroys
- E โ Entire infrastructure or specific resources
- L โ Loads state file
- E โ Ensures safe execution
- T โ Target option available
- E โ Erases resources
๐ Interview Answer: โTerraform destroy deletes all or selected infrastructure resources that Terraform manages. It ensures clean, cost-effective, and secure resource lifecycle management.โ
๐ Best Practices for Terraform Destroy
- Never use
-auto-approve
in production unless in CI/CD with full safeguards. - Always review the destroy plan carefully.
- Use
-target
cautiously to avoid accidental dependencies removal. - Backup state files before destroy operations.
- Combine with workspaces to destroy only the intended environment.
โ ๏ธ Common Mistakes with Terraform Destroy
- Running destroy on the wrong workspace.
- Forgetting dependencies, which causes multiple deletions.
- Using
-auto-approve
manually, deleting everything unintentionally. - Running destroy without state backup, leading to state corruption.
๐ฎ Future Enhancements
- Pre-destroy warnings for high-cost resources.
- Policy checks to prevent accidental production deletions.
- Undo destroy feature with snapshots.
- Graphical preview of destruction before execution.
๐ Summary
terraform destroy
removes infrastructure managed by Terraform.- Works by reading config โ loading state โ creating plan โ asking confirmation โ deleting.
- Useful for cost control, cleanup, and safe lifecycle management.
- Supports
-auto-approve
,-target
, and variable-based destruction. - Remember with D.E.L.E.T.E mnemonic.
โ Final Takeaway
Terraform Destroy is just as important as Terraform Apply. If apply is about building, destroy is about removing safely.
- For beginners, it helps clean up test environments.
- For professionals, it ensures cost-effective and secure cloud governance.
- For enterprises, it prevents resource sprawl and supports lifecycle automation.
๐ In short: Terraform Destroy = Safe Resource Cleanup with Infrastructure as Code.