Lesson 3 of 3 Planning, Applying and Destroying Infrastructure

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.

The scope of terraform destroy for the test environment. Destroy acts only on resources in this configuration's state, in reverse dependency order. The shared S3 bucket is created by another configuration and only read through a data source, so plan -destroy does not list it and destroy leaves it untouched.
The environment's resources are illustrative.

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.

GoalMethod
Destroy everything the configuration managesterraform destroy
Preview a full destroyterraform plan -destroy
Destroy one resource permanentlyRemove its block and apply
Stop managing an object but keep itremoved block with destroy = false
Match the pairs

Removing resources

Exam skills 3.6.1 · 3.6.2

Match each goal with the method that achieves it.

Log in to try this activity Interactive activities are free with an account.

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.

True or false

prevent_destroy

Exam skills 3.6.2 · 4.6.3

Check what lifecycle prevent_destroy protects against.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

Check yourself

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.