What if you could erase your entire cloud setup with a single command, no sweat?
Why Terraform destroy for cleanup? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you set up many cloud resources like servers, databases, and networks by hand for a project.
When the project ends, you need to remove all these resources manually, clicking through different consoles and deleting each one.
This manual cleanup is slow and tiring.
You might forget some resources, leaving them running and costing money.
Also, deleting resources in the wrong order can cause errors and confusion.
Using Terraform destroy lets you remove all your cloud resources automatically and safely.
It knows the order to delete things and cleans up everything you created with Terraform in one command.
Go to AWS console -> Find EC2 instance -> Terminate -> Repeat for all resourcesterraform destroy
You can clean up your entire cloud environment quickly and without mistakes, saving time and money.
A developer finishes testing a new app and runs terraform destroy to remove all test servers and databases instantly, avoiding extra cloud charges.
Manual cleanup is slow and error-prone.
Terraform destroy automates safe and complete resource removal.
This saves time, prevents mistakes, and controls costs.
Practice
terraform destroy command do?Solution
Step 1: Understand the purpose of
This command is designed to remove all resources that Terraform manages in the current workspace. Unliketerraform destroyterraform applywhich creates or updates resources,terraform destroydeletes them. It does not just show state or update resources.Final Answer:
It deletes all resources created by Terraform in the current workspace. -> Option BQuick Check:
terraform destroy= deletes resources [OK]
- Confusing destroy with apply
- Thinking destroy only shows resources
- Assuming destroy updates resources
terraform destroy without asking for confirmation?Solution
Step 1: Recall the flag to skip confirmation
The correct flag to skip the confirmation prompt is-auto-approve.-force,--yes, and--confirmare not valid flags forterraform destroy.Final Answer:
terraform destroy -auto-approve -> Option DQuick Check:
Skip confirmation = -auto-approve [OK]
- Using -force instead of -auto-approve
- Typing --yes which is invalid
- Assuming --confirm works
terraform apply -auto-approveterraform destroyWhat will happen after the second command?
Solution
Step 1: Understand the effect of
This command creates or updates resources without asking for confirmation. This command will prompt for confirmation before deleting all resources created by Terraform.terraform apply -auto-approveFinal Answer:
All resources created by the first command will be deleted after confirmation. -> Option AQuick Check:
Destroy deletes resources after confirmation [OK]
- Thinking destroy needs -auto-approve to work
- Believing destroy updates resources
- Assuming destroy must run before apply
terraform destroy but it failed with an error about a locked state file. What should you do to fix this?Solution
Step 1: Understand state locking in Terraform
Terraform locks the state file during operations to prevent conflicts. If locked, commands like destroy fail.terraform force-unlockwith the lock ID safely removes the lock so you can continue.Final Answer:
Useterraform force-unlockwith the lock ID to unlock the state. -> Option CQuick Check:
Unlock state with force-unlock command [OK]
- Deleting state file manually causing data loss
- Running terraform init which doesn't unlock state
- Manually deleting cloud resources instead of fixing state
Solution
Step 1: Identify correct command to destroy without confirmation
terraform destroy -auto-approveskips confirmation safely. Using shell redirection with> destroy.logsaves the command output to a log file.Final Answer:
Runterraform destroy -auto-approve > destroy.logto skip confirmation and save output. -> Option AQuick Check:
Destroy + auto-approve + output redirection = Runterraform destroy -auto-approve > destroy.logto skip confirmation and save output. [OK]
- Using invalid flags like --force or --log
- Confusing apply with destroy for cleanup
- Using terraform delete which is not a valid command
