0
0
TerraformHow-ToBeginner · 3 min read

How to Use Terraform Destroy: Command Syntax and Examples

Use the terraform destroy command to delete all resources defined in your Terraform configuration. Run it in your project directory where the Terraform state is stored, and confirm the action when prompted to safely remove your infrastructure.
📐

Syntax

The basic syntax of the terraform destroy command is simple and includes optional flags to customize its behavior.

  • terraform destroy: Starts the destruction process for all managed resources.
  • -auto-approve: Skips the confirmation prompt to destroy resources immediately.
  • -target=resource_address: Destroys only the specified resource.
bash
terraform destroy [options]

# Common options:
# -auto-approve  # skip confirmation
# -target=resource_address  # destroy specific resource
💻

Example

This example shows how to use terraform destroy to remove all resources defined in a simple Terraform configuration that creates an AWS S3 bucket.

hcl
provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "my-unique-bucket-123456789"
  acl    = "private"
}

# After applying with 'terraform apply', run:
# terraform destroy

# You will be prompted to confirm before destruction.
Output
Terraform will perform the following actions: - aws_s3_bucket.example Plan: 0 to add, 0 to change, 1 to destroy. Do you really want to destroy all resources? Terraform will delete all your managed infrastructure. Enter a value: yes aws_s3_bucket.example: Destroying... [id=my-unique-bucket-123456789] aws_s3_bucket.example: Destruction complete after 2s Destroy complete! Resources: 1 destroyed.
⚠️

Common Pitfalls

Common mistakes when using terraform destroy include:

  • Running terraform destroy in the wrong directory without the correct state file, which causes no resources to be destroyed.
  • Not using -auto-approve when scripting, causing the process to hang waiting for confirmation.
  • Accidentally destroying all resources when only a specific resource should be removed.

Always double-check your current directory and state before running the command.

bash
## Wrong: destroys everything unintentionally
terraform destroy

## Right: destroy only one resource
terraform destroy -target=aws_s3_bucket.example

## Right: auto approve in scripts
terraform destroy -auto-approve
📊

Quick Reference

Here is a quick summary of useful terraform destroy options:

OptionDescription
terraform destroyDestroys all resources in the current state
-auto-approveSkips confirmation prompt
-target=resource_addressDestroys only the specified resource
-refresh=true|falseRefreshes state before destroying (default true)

Key Takeaways

Run terraform destroy in the directory with your Terraform state to remove managed resources.
Use -auto-approve to skip confirmation in automated scripts.
Use -target to destroy specific resources without affecting others.
Always verify your current directory and state before destroying to avoid accidental deletion.
Terraform will prompt for confirmation unless -auto-approve is used.