TerraformPilot

Terraform

How to Destroy a Single Resource in Terraform

Destroy individual Terraform resources without affecting the rest. Covers -target flag, state rm, replace, and lifecycle prevent_destroy.

LLuca Berton1 min read

Quick Answer

#

Use terraform destroy -target=RESOURCE_ADDRESS to destroy a single resource. For example: terraform destroy -target=aws_instance.web. This leaves all other resources untouched.

Method 1: Target Destroy

#
# Destroy one specific resource
terraform destroy -target=aws_instance.web
 
# Destroy a module instance
terraform destroy -target=module.app
 
# Destroy an indexed resource
terraform destroy -target='aws_subnet.private[0]'
terraform destroy -target='aws_subnet.private["us-east-1a"]'
 
# Preview first (plan)
terraform plan -destroy -target=aws_instance.web

Method 2: Remove from Config + Apply

#
# 1. Delete or comment out the resource block
# resource "aws_instance" "web" { ... }
 
# 2. Apply — Terraform destroys the removed resource
terraform apply

This is the cleanest approach — config and state stay in sync.

Method 3: Remove from State Only

#
# Remove from state WITHOUT destroying the cloud resource
terraform state rm aws_instance.web
 
# The resource stays in AWS but Terraform forgets about it
# Use this when you want to stop managing a resource

Method 4: Replace (Destroy + Recreate)

#
# Terraform 1.4+: replace a resource (destroy old, create new)
terraform apply -replace=aws_instance.web
 
# Older versions: taint then apply
terraform taint aws_instance.web
terraform apply

Method 5: removed Block (Terraform 1.7+)

#
# Tell Terraform to forget this resource without destroying it
removed {
  from = aws_instance.web
  lifecycle {
    destroy = false
  }
}

Prevent Accidental Destruction

#
resource "aws_db_instance" "main" {
  # ...
 
  lifecycle {
    prevent_destroy = true  # terraform destroy will fail for this resource
  }
}
terraform destroy -target=aws_db_instance.main
# Error: Instance cannot be destroyed
# To override: remove prevent_destroy, then destroy

Common Patterns

#
GoalCommand
Destroy one resourceterraform destroy -target=aws_instance.web
Destroy a moduleterraform destroy -target=module.app
Stop managing (keep in cloud)terraform state rm aws_instance.web
Force recreateterraform apply -replace=aws_instance.web
Remove from configDelete block, then terraform apply

Safety Tips

#
  • Always plan -destroy -target first — review what will be destroyed
  • Use prevent_destroy on databases, S3 buckets, and other critical resources
  • -target is for surgical operations — don't use it as your normal workflow
  • Removing from state doesn't delete the resource — it just makes Terraform forget it
#

Conclusion

#

terraform destroy -target is the quickest way to remove a single resource. For a cleaner workflow, remove the resource block from config and run terraform apply. Use terraform state rm when you want Terraform to stop managing a resource without destroying it in the cloud.

#Terraform#DevOps#Infrastructure as Code

Share this article