Terraform · Infrastructure as code
How do you destroy infrastructure safely?
Short answer
terraform destroy removes all resources in the configuration. Plan the destroy first, protect critical resources with prevent_destroy, and remember that removing a block and destroying are different operations.
Destroy runs the same graph in reverse, deleting dependent resources before their dependencies. It is appropriate for ephemeral environments and teardown, but dangerous when pointed at production or when the state does not match what you think it manages.
terraform plan -destroy -out=destroy.tfplan
terraform show destroy.tfplan
terraform apply destroy.tfplanSafety practices
- Confirm account, region and workspace before running destroy
- Use prevent_destroy on databases, buckets with data and DNS zones
- Take backups or snapshots that outlive the destroy
- Prefer destroying ephemeral environments through the same CI pipeline
Removing a resource block and running apply also destroys that resource. This is easy to miss during a refactor. If the intent is to stop managing an object but keep it, remove it from state with terraform state rm or a removed block instead of deleting the configuration.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What is the difference between terraform destroy and removing a resource block?
- 2How do you stop managing a resource without deleting it?
- 3How does prevent_destroy behave during destroy?
- 4In what order are resources destroyed?
Say it in the interview
I destroy through a saved destroy plan after confirming account, region and workspace. Critical resources get prevent_destroy and independent backups, because destroy is not a rollback. I also distinguish between deleting a block, which destroys the resource, and removing it from state, which leaves it alive but unmanaged. Ephemeral environments should be torn down through the same pipeline that created them.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Terraform mock interview and get scored feedback on depth, clarity and confidence.