Last Updated on August 7, 2025 by Arnav Sharma
When managing cloud infrastructure, understanding the lifecycle of resources is crucial. Terraform, a powerful tool by HashiCorp, simplifies this process. Particularly, the “destroy” command in Terraform is essential when you need to delete or decommission resources. In this blog, we will delve into how to use Terraform to destroy infrastructure, especially focusing on examples from Azure.
What is Terraform?
Terraform, developed by HashiCorp, is an infrastructure as code (IaC) tool used to provision and manage cloud infrastructure. Using a terraform script, you can define, deploy, and update resources across various cloud platforms, including AWS and Azure.
Terraform Workflow: Plan, Apply, and Destroy
The Terraform workflow typically involves three steps:
- Terraform Plan: Creates an execution plan to determine what actions are necessary to achieve the desired state specified in the terraform configuration files.
- Terraform Apply: Applies the changes necessary to reach the desired state of the configuration.
- Terraform Destroy: Used to remove resources that are no longer needed.
Understanding the Destroy Command
The terraform destroy command is a critical part of managing the lifecycle of your infrastructure. It’s used when you want to remove any provisioned infrastructure that you no longer need, helping to reduce your security exposure and manage costs.
How it Works
- Specify the Script: You start by specifying the terraform script that outlines the infrastructure you wish to remove.
- Creating a Plan: Terraform then creates a plan showing what will be removed.
- Execution: Finally, upon approval, Terraform will destroy the specified resources.
Use Cases for Terraform Destroy
- Reducing Costs: When you no longer need infrastructure, destroying it helps reduce costs associated with idle resources.
- Environment Management: For managing short-lived environments like build or test environments, terraform destroy helps in decommissioning them once they are no longer needed.
- Updating Infrastructure: In cases where it’s easier to recreate the infrastructure from scratch rather than updating it, terraform destroy is used to remove the old infrastructure before provisioning new resources.
Destroying Azure Resources with Terraform
Azure, like AWS, can be managed using Terraform. Whether it’s an EC2 instance in AWS or a VM in Azure, Terraform provides a unified way to manage these resources.
Destroying an Azure VM
- Define the Resource: In your Terraform script, you would have defined an Azure VM.
- Plan and Apply: You would have initially used
terraform planandterraform applyto provision the VM. - Destroy: To remove this VM, you would update your Terraform configuration to reflect that the VM is no longer needed and then run
terraform destroy.
Best Practices for Using Terraform Destroy
- Backup State File: Always backup your Terraform state file before running the destroy command.
- Review Execution Plan: Carefully review the execution plan created by Terraform to ensure only the intended resources are destroyed.
- Manage Dependencies: Terraform determines the order of resource destruction based on dependencies. Ensure that your configurations correctly reflect these dependencies.
- Use Version Control: Store your Terraform configurations in a version control system like GitHub for better management and tracking.
Example 1: Delete a Simple Azure Resource
Suppose you have a Terraform configuration that defines an Azure virtual machine (VM). Here’s how you would typically destroy this VM:
- Initial Setup: You should have a Terraform script (
main.tf) that defines an Azure VM. For example:resource "azurerm_virtual_machine" "example_vm"{
# VM configuration details
}
- Plan and Apply: Initially, you would use
terraform planandterraform applyto provision this VM. Destroy the VM: When you decide to remove this VM, you would use the
terraform destroycommand. Here’s how:- Open your command line.
- Navigate to the directory containing your Terraform configuration files.
- Run the command:
terraform destroy - Terraform will provide an execution plan and ask for confirmation before destroying the resources.
Example 2: Destroying Specific Resources in Azure
Sometimes, you might want to destroy a specific resource within your Terraform-managed infrastructure without affecting other resources.
- Identify the Resource: First, identify the resource you want to destroy. Each resource in your Terraform state file has an address. You can list all resources with their addresses using:
terraform state list - Destroy the Specific Resource: Use the
terraform destroycommand with the-targetoption. For instance, to destroy a specific Azure network interface:terraform destroy -target azurerm_network_interface.example_nicThis command will only destroy the resource specified and leave others intact
Example 3: Destroying All Resources in a Terraform Project
If you need to completely tear down all the infrastructure managed by a Terraform project, you simply run the terraform destroy command without specifying a target. This is common when decommissioning an entire environment.
- Execute Terraform Destroy:
- Navigate to your project directory.
- Run:
terraform destroy - Review the plan and confirm the destruction.
I help organisations secure their cloud infrastructure and stay ahead of evolving cyber threats. Microsoft MVP and Certified Trainer, author of Mastering Azure Security, and founder of arnav.au — a platform for practical Cloud, Cybersecurity, DevOps and AI content.
Frequently Asked Questions
The terraform destroy command is used to remove provisioned infrastructure that is no longer needed. It should be used when you want to reduce costs associated with idle resources, decommission short-lived environments like test or build environments, or completely tear down infrastructure before recreating it from scratch.
To destroy a specific resource, use the terraform destroy command with the -target option. For example, `terraform destroy -target azurerm_network_interface.example_nic` will only destroy that specific resource while leaving others intact. First, you can list all resources using `terraform state list` to identify the resource address.
To destroy an Azure VM, first ensure it was defined in your Terraform script and provisioned using terraform plan and terraform apply. Then, update your configuration to reflect that the VM is no longer needed and run the terraform destroy command from your terminal. Terraform will show an execution plan and ask for confirmation before removing the VM.
Key best practices include backing up your Terraform state file before running the destroy command, carefully reviewing the execution plan to ensure only intended resources are being destroyed, ensuring your configurations correctly reflect resource dependencies, and storing your Terraform configurations in a version control system like GitHub for tracking and management.
The Terraform workflow consists of three main steps: terraform plan (creates an execution plan to determine necessary actions), terraform apply (applies changes to reach the desired state), and terraform destroy (removes resources no longer needed). This three-step process allows you to manage the complete lifecycle of your infrastructure from creation through decommissioning.