Terraform destroy for cleanup - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we run terraform destroy, it removes resources created by terraform. We want to understand how the time to clean up grows as we have more resources.
The question is: How does the number of resources affect the cleanup time?
Analyze the time complexity of the following operation sequence.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
}
// terraform destroy will remove all instances created
This code creates multiple instances based on a count variable. Destroy will remove all these instances.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: API call to delete each instance resource.
- How many times: Once per instance, so equal to the number of instances.
As the number of instances increases, the number of delete calls grows at the same rate.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 delete calls |
| 100 | 100 delete calls |
| 1000 | 1000 delete calls |
Pattern observation: The cleanup work grows directly with the number of resources.
Time Complexity: O(n)
This means the time to destroy grows linearly with the number of resources to remove.
[X] Wrong: "Destroying many resources takes the same time as destroying one."
[OK] Correct: Each resource requires a separate delete call, so more resources mean more work and more time.
Understanding how resource cleanup scales helps you plan infrastructure changes and estimate deployment times. This skill shows you think about real-world impacts of your code.
"What if terraform destroy could delete multiple resources in one API call? How would the time complexity change?"
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
