You have now seen how to build and change infrastructure. Before moving on to creating multiple resources and showing resource dependencies, you will see how to completely destroy your Terraform-managed infrastructure.
Destroying your infrastructure is a rare event in production environments. But if you're using Terraform to spin up multiple environments such as development, testing, and staging, then destroying is often a useful action.
»Destroy
Resources can be destroyed using the terraform destroy
command, which is
similar to terraform apply
but it behaves as if all of the resources have been
removed from the configuration.
Destroy all the infrastructure you've created in this track.
$ terraform destroy
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# google_compute_instance.vm_instance will be destroyed
- resource "google_compute_instance" "vm_instance" {
- can_ip_forward = false -> null
- cpu_platform = "Intel Haswell" -> null
- deletion_protection = false -> null
- enable_display = false -> null
- guest_accelerator = [] -> null
- id = "projects/testing-project/zones/us-central1-c/instances/terraform-instance" -> null
- instance_id = "1820538232654796756" -> null
- label_fingerprint = "42WmSpB8rSM=" -> null
- machine_type = "f1-micro" -> null
## ... Output Truncated ...
}
# google_compute_network.vpc_network will be destroyed
- resource "google_compute_network" "vpc_network" {
- auto_create_subnetworks = true -> null
- delete_default_routes_on_create = false -> null
- id = "projects/testing-project/global/networks/terraform-network" -> null
- name = "terraform-network" -> null
- project = "testing-project" -> null
- routing_mode = "REGIONAL" -> null
- self_link = "https://www.googleapis.com/compute/v1/projects/testing-project/global/networks/terraform-network" -> null
}
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value:
The -
prefix indicates that the instance and the network will be destroyed. 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.
Enter a value: yes
google_compute_instance.vm_instance: Destroying... [id=projects/testing-project/zones/us-central1-c/instances/terraform-instance]
google_compute_instance.vm_instance: Still destroying... [id=projects/testing-project/zones/...entral1-c/instances/terraform-instance, 10s elapsed]
google_compute_instance.vm_instance: Destruction complete after 16s
google_compute_network.vpc_network: Destroying... [id=projects/testing-project/global/networks/terraform-network]
google_compute_network.vpc_network: Still destroying... [id=projects/testing-project/global/networks/terraform-network, 10s elapsed]
google_compute_network.vpc_network: Still destroying... [id=projects/testing-project/global/networks/terraform-network, 20s elapsed]
google_compute_network.vpc_network: Still destroying... [id=projects/testing-project/global/networks/terraform-network, 30s elapsed]
google_compute_network.vpc_network: Destruction complete after 37s
Destroy complete! Resources: 2 destroyed.
Just like with terraform apply
, Terraform determines the order in which
resources must be destroyed. GCP won't allow a VPC network to be deleted if
there are other resources still in it, so Terraform waits until the instance is
destroyed before destroying the network. 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.