0
0
Terraformcloud~5 mins

Terraform destroy for cleanup - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes after creating cloud resources with Terraform, you want to remove them to avoid extra costs or keep your environment clean. Terraform destroy helps you safely delete all resources it created.
When you finish testing infrastructure and want to remove all created resources.
When you want to reset your environment to start fresh without leftover resources.
When you want to avoid paying for cloud resources you no longer need.
When you want to clean up resources after a demo or workshop.
When you want to delete a temporary environment created for a short project.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

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

resource "aws_s3_bucket" "example_bucket" {
  bucket = "example-terraform-destroy-bucket-12345"
  acl    = "private"
}

This file tells Terraform to use the AWS provider in the us-east-1 region. It creates one S3 bucket named "example-terraform-destroy-bucket-12345". This simple resource is what we will create and later destroy.

Commands
This command sets up Terraform in the current folder by downloading the AWS provider plugin. It prepares Terraform to work with the configuration.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command creates the resources defined in the configuration file. The -auto-approve flag skips the confirmation prompt to apply changes immediately.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 2s [id=example-terraform-destroy-bucket-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command deletes all resources created by Terraform in this folder. The -auto-approve flag skips the confirmation prompt to destroy immediately.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Destroying... [id=example-terraform-destroy-bucket-12345] aws_s3_bucket.example_bucket: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.
-auto-approve - Automatically approve the destroy without asking for confirmation
This command lists all resources Terraform currently manages. After destroy, it should show no resources.
Terminal
terraform state list
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: terraform destroy safely removes all resources Terraform created to clean up your environment.

Common Mistakes
Running terraform destroy without confirming the prompt and without -auto-approve flag.
It pauses and waits for manual confirmation, which can confuse beginners expecting immediate action.
Use -auto-approve flag to skip confirmation if you want to automate or speed up the process.
Running terraform destroy in the wrong folder or workspace.
It may destroy resources you did not intend to delete, causing data loss or downtime.
Always check your current directory and workspace with terraform workspace show before destroying.
Not running terraform init before terraform destroy in a new or cleaned folder.
Terraform will not have the provider plugins and will fail to run commands.
Run terraform init first to prepare the working directory.
Summary
terraform init prepares Terraform to work with your cloud provider.
terraform apply creates the resources defined in your configuration.
terraform destroy deletes all resources Terraform manages to clean up.
Use -auto-approve to skip confirmation prompts for apply and destroy.
Check your Terraform state to confirm resources are created or removed.