Destroy
Theterraform destroy command destroys all resources managed by your Terraform configuration. It’s a convenience alias for terraform apply -destroy.
What It Does
When you runterraform destroy, Terraform:
- Generates a destruction plan showing all resources to be deleted
- Determines the correct destruction order based on dependencies
- Prompts for confirmation before proceeding
- Destroys resources in the proper sequence
- Updates the state file to reflect removed resources
- Removes all managed infrastructure while preserving the state file
When to Use It
Runterraform destroy when you want to:
- Tear down temporary development or testing environments
- Remove all infrastructure in a workspace before deletion
- Clean up resources after a project is complete
- Start fresh with a clean slate
- Decommission an entire environment
Basic Usage
Common Flags and Options
Auto-Approval
-auto-approve
Skip the confirmation prompt:
Targeting Resources
-target=RESOURCE
Destroy only specific resources:
State Management
-backup=PATH
Specify a custom backup location for the state file:
-lock=false
Disable state locking (dangerous):
-lock-timeout=DURATION
Wait for a state lock:
Output Control
-no-color
Disable colored output:
-compact-warnings
Show warnings in compact form:
Performance
-parallelism=N
Limit concurrent destroy operations (default: 10):
Best Practices
Always Preview First
Review what will be destroyed:Verify Workspace
Ensure you’re in the correct workspace:Backup State Before Destruction
Create a manual backup:Protect Critical Resources
Use lifecycle rules to prevent accidental destruction:Incremental Destruction
Destroy resources in stages using-target:
Avoid Auto-Approve in Production
Never use-auto-approve for production environments:
Handle Dependencies
Terraform automatically handles dependencies:Data Backup
Backup critical data before destroying:CI/CD Considerations
In automated environments, add safeguards:Workspace Naming
Use clear workspace names to prevent mistakes:Partial Destruction
Removing Individual Resources
Remove specific resources from state without destroying:Destroying Specific Modules
Destroy resources within a module:Understanding Destroy Output
Destruction Progress
Dependency Order
Terraform destroys in reverse dependency order:Summary Line
Troubleshooting
Cannot Destroy Due to Dependencies
Resources Already Deleted
Stuck Destroy Operation
If destroy hangs:Prevent Destroy Protection
State Lock During Destroy
Orphaned Resources
If resources are orphaned after partial destroy:Recovery from Failed Destroy
If destroy fails partway through:Alternative to Destroy
Using Terraform Apply
Destroy can also be done via apply:terraform destroy.
Removing Resources from Configuration
Instead of destroying everything:- Remove unwanted resources from
.tffiles - Run
terraform apply - Terraform will destroy removed resources
Next Steps
After destroying infrastructure:- Verify resources are deleted in your cloud provider console
- Consider deleting the workspace:
terraform workspace delete <name> - Archive or delete state files if no longer needed
- Clean up any manual resources not managed by Terraform
- Document the destruction for audit purposes