terraform destroy – Infrastructure as Code (IaC) with Terraform

To destroy the resource group, we can run a speculative plan first. It is always a best practice to run a speculative plan to confirm that what we need to destroy is within the output to have no surprises later. Terraform, like Linux, does not have an undo button.

To run a speculative destroy plan, use the following command:
$ terraform plan -destroy

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
destroy

Terraform will perform the following actions:

azurerm_resource_group.rg will be destroyed – resource “azurerm_resource_group” “rg” {
id = “/subscriptions/id/resourceGroups/terraform-exercise” -> null
location = “westeurope” -> null
name = “terraform-exercise” -> null
tags = {} -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.

As we see, as the resource group was the only resource managed by Terraform, it has listed that as the resource that will be destroyed. There are two ways of destroying the resource: using terraform destroy on its own or saving the speculative plan using the out parameter and running terraform apply on the destroy plan.

Let’s use the first method for now.

Run the following command to destroy all resources managed by Terraform:
$ terraform destroy

Terraform will perform the following actions:
azurerm_resource_group.rg will be destroyed 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:
Now, this time, Terraform reruns plan and prompts for a value. It will only accept yes as confirmation.
So, you can review the output, type yes, and hit Enter to confirm:
Enter a value: yes
azurerm_resource_group.rg: Destroying… [id=/subscriptions/id/resourceGroups/terraform-exercise]
azurerm_resource_group.rg: Still destroying… [id=/subscriptions/id/resourceGroups/ terraform-exercise, 10s elapsed]
azurerm_resource_group.rg: Destruction complete after 1m20s

And it has now destroyed the resource group.

We’ve looked at a basic root module and explored the Terraform workflow. The basic root module helps us create and manage resources but lacks a very important feature – reusability. Terraform provides us with modules to allow reusability for common templates. Let’s look at it in the next section.

Leave a Reply

Your email address will not be published. Required fields are marked *