Destroying Infrastructure
terraform destroy removes everything a configuration manages. This lesson explains how destroy works, how to preview it, how to remove single resources and how to protect critical ones.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how terraform destroy works and how to preview it.
- Choose a method for removing a single resource.
- Describe how prevent_destroy protects resources.
How destroy works
terraform destroy is an alias for terraform apply -destroy. It plans the destruction of every resource instance in the current state, shows the plan and asks for confirmation. Resources are destroyed in reverse dependency order: instances before the subnets they use, subnets before the network. -auto-approve skips the prompt.
Destroy only affects objects in state. Resources created outside Terraform, resources managed by other configurations and data sources, which Terraform only reads, are never touched. To preview a destroy without changing anything, run terraform plan -destroy, optionally with -out to save it for review and apply it later with terraform apply.
The following diagram shows the scope of destroy.

Removing one resource
To destroy a single resource, the normal method is to remove its block from the configuration and apply. The plan shows that resource, and only that resource, being destroyed. Reducing count or removing a key from a for_each collection destroys the corresponding instances in the same way.
terraform destroy -target=ADDRESS destroys a resource and everything that depends on it, but it is intended for exceptional situations. If the block stays in the configuration, the next plan creates the resource again. To stop managing an object without destroying it, use a removed block with destroy = false.
| Goal | Method |
|---|---|
| Destroy everything the configuration manages | terraform destroy |
| Preview a full destroy | terraform plan -destroy |
| Destroy one resource permanently | Remove its block and apply |
| Stop managing an object but keep it | removed block with destroy = false |
Removing resources
Protecting critical resources
lifecycle { prevent_destroy = true } on a resource makes Terraform reject any plan that would destroy it, including terraform destroy and replacements forced by argument changes. To destroy it deliberately, first remove the setting from the configuration. The setting protects against mistakes in Terraform only; it does not stop deletion through the platform's console, so combine it with the platform's own protection, such as deletion protection on databases.
prevent_destroy lives in the block
If the resource block is removed from the configuration, its prevent_destroy setting is removed too, and the next apply destroys the object.
prevent_destroy
Tearing down a test environment
A test environment shares an S3 bucket created by a separate configuration, and reads it with a data source. The team wants to destroy the test environment without affecting the bucket.
The decisive fact is that destroy only acts on resources in this configuration's state.
Running terraform plan -destroy first confirms the scope: the bucket does not appear, because it is only read through a data source. terraform destroy then removes the environment's resources in reverse dependency order, and the shared bucket is untouched.
Which command previews what terraform destroy would remove without changing anything?
Show answer
terraform plan -destroy.
Exam Tips
Remember for the exam
- destroy = apply -destroy; plan -destroy previews it.
- Only resources in state are destroyed; data sources never are.
- Remove a block and apply to destroy one resource.
- prevent_destroy makes destroy plans fail while the block exists.