Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

terraform

What is the command to destroy infrastructure in Terraform?

Faraz Karim

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The ‘destroy’ command

A terraform project uses up system resources until it is terminated. The terraform destroy command is used to destroy the project infrastructure and free up the allocated resources. terraform destroy only destroys the infrastructure for the current project. There are no changes made to the resources allocations running elsewhere.

Syntax

Following is the command:

terraform destroy <additional_options>

In the above command, the additional_options argument can be replaced by several possible flags to gain further control over the terraform destroy command functionality.

Targeted destruction

Just running terraform destroy destroys the whole project infrastructure, releasing all resources. Sometimes you need to destroy just a select few resources, and for that, you can use the -target flag. When used with the -target flag, the terraform destroy command takes the resource name as its input and destroys just that specific resource upon execution. Following is how you would use it:

terraform destroy -target <resource_name>

Alternate command

The terraform destroy command can be considered an alias for the terraform apply command with the -destroy flag.

terraform apply -destroy

This command runs terraform apply in the destroy planning mode.

Following steps

After running either one of the commands above, terraform proceeds to present a destruction plan, which shows you all the resources that will be destroyed. If you approve of this plan, terraform destroyed the stated resources. Otherwise, the destroy command does not get executed.

RELATED TAGS

terraform

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring