Getting Started - Google Cloud Platform

Destroy Infrastructure

We've now seen how to build and change infrastructure. Before we move on to creating multiple resources and showing resource dependencies, we're going to go over 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, test, and QA, 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.

$ terraform destroy
google_compute_network.vpc_network: Refreshing state... [id=terraform-network]
google_compute_instance.vm_instance: Refreshing state... [id=terraform-instance]

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
...

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:

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: yes

google_compute_instance.vm_instance: Destroying... [id=terraform-instance]
google_compute_instance.vm_instance: Still destroying... [id=terraform-instance, 10s elapsed]
google_compute_instance.vm_instance: Still destroying... [id=terraform-instance, 20s elapsed]
google_compute_instance.vm_instance: Still destroying... [id=terraform-instance, 30s elapsed]
google_compute_instance.vm_instance: Still destroying... [id=terraform-instance, 40s elapsed]
google_compute_instance.vm_instance: Still destroying... [id=terraform-instance, 50s elapsed]
google_compute_instance.vm_instance: Still destroying... [id=terraform-instance, 1m0s elapsed]
google_compute_instance.vm_instance: Destruction complete after 1m04s
google_compute_network.vpc_network: Destroying... [id=terraform-network]
google_compute_network.vpc_network: Still destroying... [id=terraform-network, 10s elapsed]
google_compute_network.vpc_network: Still destroying... [id=terraform-network, 20s elapsed]
google_compute_network.vpc_network: Still destroying... [id=terraform-network, 30s elapsed]
google_compute_network.vpc_network: Still destroying... [id=terraform-network, 40s elapsed]
google_compute_network.vpc_network: Still destroying... [id=terraform-network, 50s elapsed]
google_compute_network.vpc_network: Still destroying... [id=terraform-network, 1m0s elapsed]
google_compute_network.vpc_network: Still destroying... [id=terraform-network, 1m10s elapsed]
google_compute_network.vpc_network: Destruction complete after 1m17s

Destroy complete! Resources: 2 destroyed.

Just like with apply, Terraform determines the order in which things must be destroyed. GCP won't allow a VPC network to be deleted if there are 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.