Terraform
Destroy infrastructure
You have now used Terraform to create and update Azure resources. In this tutorial, you will use Terraform to destroy your infrastructure.
Once you no longer need infrastructure, you may 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 infrastructure it manages.
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.
Destroy the resources you created. When Terraform prompts you, type yes
to execute this
plan and destroy the infrastructure.
$ terraform destroy
azurerm_resource_group.rg: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]
azurerm_virtual_network.vnet: Refreshing state... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet]
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/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup" -> null
- location = "westus2" -> null
- name = "myTFResourceGroup" -> null
- tags = {
- "Environment" = "Terraform Getting Started"
- "Team" = "DevOps"
} -> null
}
# azurerm_virtual_network.vnet will be destroyed
- resource "azurerm_virtual_network" "vnet" {
- address_space = [
- "10.0.0.0/16",
] -> null
- dns_servers = [] -> null
- guid = "1a4efd63-61e5-48bb-ad9d-9ceadd403ecb" -> null
- id = "/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet" -> null
- location = "westus2" -> null
- name = "myTFVnet" -> null
- resource_group_name = "myTFResourceGroup" -> null
- subnet = [] -> null
- tags = {} -> null
- vm_protection_enabled = 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: yes
azurerm_virtual_network.vnet: Destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup/providers/Microsoft.Network/virtualNetworks/myTFVnet]
azurerm_virtual_network.vnet: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...osoft.Network/virtualNetworks/myTFVnet, 10s elapsed]
azurerm_virtual_network.vnet: Destruction complete after 11s
azurerm_resource_group.rg: Destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-a322114dfb29/resourceGroups/myTFResourceGroup]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...29/resourceGroups/myTFResourceGroup, 10s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...29/resourceGroups/myTFResourceGroup, 20s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...29/resourceGroups/myTFResourceGroup, 30s elapsed]
azurerm_resource_group.rg: Still destroying... [id=/subscriptions/c9ed8610-47a3-4107-a2b2-...29/resourceGroups/myTFResourceGroup, 40s elapsed]
azurerm_resource_group.rg: Destruction complete after 47s
Destroy complete! Resources: 2 destroyed.
The -
prefix in the output indicates that the instance will be destroyed. As with apply
, Terraform shows its execution plan and waits for approval before making any changes.
Just like with apply, Terraform determines the order in which things must be destroyed. In more complicated cases with multiple resources, Terraform will destroy them in a suitable order to respect dependencies.