Last Updated on August 7, 2025 by Arnav Sharma
Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define, provision, and manage infrastructure in a consistent and repeatable way. One of the key operations in Terraform is terraform destroy, which is used to terminate all the resources defined in your configuration. This blog post will explain how terraform destroy works, using Azure examples to illustrate the process.
Understanding terraform destroy
The terraform destroy command is designed to delete all the resources that were created by a particular Terraform configuration. It ensures that all the infrastructure components are safely and completely removed, cleaning up the environment.
Basic Workflow of terraform destroy
- Plan the Destruction: Terraform creates a plan to destroy the resources.
- Confirm the Plan: You review and confirm the plan.
- Execute the Plan: Terraform destroys the resources according to the plan.
Key Concepts
- State File: Terraform maintains a state file that tracks the current state of your infrastructure. This file is crucial for the
terraform destroyoperation as it references this file to determine what needs to be destroyed. - Dependencies: Terraform understands the dependencies between resources. When destroying resources, it ensures that dependent resources are destroyed in the correct order.
Example: Destroying Resources in Azure
Let’s walk through an example of using terraform destroy with Azure. We’ll assume you have already created some resources using Terraform.
Sample Terraform Configuration
provider “azurerm” {
features {}
}
resource “azurerm_resource_group” “example” {
name = “example-resources”
location = “West Europe”
}
resource “azurerm_storage_account” “example” {
name = “examplestoracc”
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = “Standard”
account_replication_type = “LRS”
}
In this configuration, we have an Azure Resource Group and a Storage Account.
Step-by-Step Guide to Using terraform destroy
- Initialize Terraform: First, ensure that your Terraform environment is initialized using terraform init
- Apply the Configuration: Before destroying resources, make sure they are created using terraform apply
- Destroy the Resources: To destroy the resources, run: terraform destroy
Terraform will prompt you to confirm the destruction plan:
Do you really want to destroy all resources?
Terraform will perform the following actions:
- azurerm_resource_group.example will be destroyed
- azurerm_storage_account.example will be destroyed
Plan: 0 to add, 0 to change, 2 to destroy.
Do you want to perform these actions?
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
Review the Plan and Confirm:
Type yes and press Enter. Terraform will proceed to destroy the resources as planned.
azurerm_resource_group.example: Destroying... [id=/subscriptions/.../resourceGroups/example-resources]
azurerm_storage_account.example: Destroying... [id=/subscriptions/.../resourceGroups/example-resources/providers/Microsoft.Storage/storageAccounts/examplestoracc]
Destroy complete! Resources: 2 destroyed.
Handling Errors and Dependencies
Terraform takes care of dependencies between resources. For example, if you have resources dependent on the Azure Storage Account, Terraform will destroy those dependent resources first before destroying the Storage Account.
Best Practices for Using terraform destroy
- Review the Plan Carefully: Always review the destruction plan carefully before confirming, especially in production environments.
- Back Up State Files: Ensure you have backups of your state files to avoid accidental data loss.
- Use with Caution in Production: Use
terraform destroywith caution in production environments. Consider using targeted destruction with the-targetoption to destroy specific resources. - Automate Safely: If automating destruction, ensure there are safeguards to prevent accidental data loss.
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 designed to delete all the resources that were created by a particular Terraform configuration. It ensures that all infrastructure components are safely and completely removed from your environment by referencing the state file to determine what needs to be destroyed.
Terraform understands the dependencies between resources and ensures that dependent resources are destroyed in the correct order. For example, if you have resources dependent on an Azure Storage Account, Terraform will destroy those dependent resources first before destroying the Storage Account itself.
The process involves three main steps: first, initialize Terraform using terraform init, then apply the configuration using terraform apply to create resources, and finally run terraform destroy to remove them. Terraform will create a destruction plan that you must review and confirm by typing 'yes' before the resources are actually deleted.
The state file is crucial for the terraform destroy operation because Terraform references it to determine what resources need to be destroyed. Without an accurate state file, Terraform would not know which resources to remove from your infrastructure.
Key best practices include reviewing the destruction plan carefully before confirming, backing up your state files to prevent accidental data loss, using the -target option for targeted destruction of specific resources, and implementing safeguards if automating the process to prevent unintended resource deletion.