Destroy infrastructure
You have created and modified infrastructure using Terraform. You will now learn how to destroy your Terraform-managed infrastructure.
Once you no longer need infrastructure, you might want to destroy it to reduce your security exposure and costs. For example, you may remove a production environment from service, or manage short-lived environments like build or testing systems. In addition to building and modifying infrastructure, Terraform can destroy or recreate the resources it manages.
Note
This tutorial builds on configuration from the previous tutorial. If you have not yet completed it, do so now.
Destroy
The terraform destroy
command terminates resources managed by your Terraform
project. This command is the inverse of terraform apply
in that it terminates
all the resources specified in your Terraform state. It does not destroy
resources running elsewhere that are not managed by the current Terraform
project.
The -
prefix indicates that Terraform will destroy the instance and the network. As
with apply, Terraform shows its execution plan and waits for approval before
making any changes.
Answer yes
to execute this plan and destroy the infrastructure.
As with terraform apply
, Terraform determines the order in which
resources must be destroyed. GCP will not destroy a VPC network if
there are other resources still in it, so Terraform waits until the instance is
destroyed first. When performing operations, Terraform
creates a dependency graph to determine the correct order of operations. In more
complicated cases with multiple resources, Terraform will perform operations in
parallel when it's safe to do so.