terraform destroy uses state to unwind the dependency graph and delete managed resources. It is convenient, but misuse can cause major outages. This article organizes safe deletion and proper use of targeted removal so you can pick it up quickly from both exam and operational angles.
The first half covers safe-confirmation flows and -target caveats; the second half covers guardrail setup, workspaces, and how to handle failures. The syntax and behavior align with stable features in the official Terraform documentation.
terraform destroy proceeds through three stages: plan → confirm → apply. The plan stage determines the deletion order from current state and configuration and computes the sequence of delete requests to providers. The confirm stage proceeds based on the interactive prompt or whether -auto-approve is present. The apply stage executes deletions according to the dependency graph.
The basics of safety are: make the plan visible before deletion, back up state, and confirm locking. On top of that, minimize the scope of what actually gets deleted, and split the order with targeted removal when needed.
Dependency graph and deletion order (example)
Standard practice for safe pre-checks and backups
terraform workspace show
terraform state pull > state-backup-$(date +%Y%m%d%H%M%S).json
terraform plan -destroy -out=tfplan
terraform show tfplan-target is an advanced option that limits the plan's search scope. The official recommendation is to use it only for exceptional cases like emergency workarounds or staged deletion. Dependents (upstream resources that depend on the target) are not included in the plan, which can cause orphaning or temporary outages.
You can combine multiple -target flags. Resources accessed via modules or with count/for_each require exact address syntax. If you mistakenly remove via state rm, the cloud-side entity remains and drift increases.
Example of staged deletion (minimizing impact)
# First, separate the most-related upstream resources
terraform plan -destroy -target=aws_api_gateway_rest_api.main -out=tfplan1
terraform destroy -target=aws_api_gateway_rest_api.main -auto-approve
# Next, delete the execution layer
terraform plan -destroy -target=aws_lambda_function.fn -out=tfplan2
terraform destroy -target=aws_lambda_function.fn -auto-approve
# Finally, the foundational permissions
terraform destroy -target=aws_iam_role.app -auto-approveEach deletion approach has its own characteristics. For the Associate exam, it is important to identify which approach actually deletes infrastructure and which only changes state.
In operations, the safe order is: first the orthodox path (full-deletion plan → apply), then targeted removal only when unavoidable, and import/state operations carefully when resolving drift or eliminating duplicate management.
| Method | Scope of effect | Main benefits | Risks / cautions |
|---|---|---|---|
| terraform destroy | All managed resources | Bulk deletion in a safe order along the dependency graph | Risk of running against the wrong environment. Always verify workspace and plan |
| terraform destroy -target=addr | The specified address plus required dependencies | Localizes impact and lets you stage the order | Dependents are not included, risking orphaning. Not recommended for permanent operations |
| terraform state rm addr | Terraform state only | Useful for resolving mis-registration or duplicate management | The cloud entity remains, causing drift |
| Manual deletion via cloud console | Only the target resource (external operation) | Responds immediately when emergency stop or forced action is needed | State drifts; import/refresh is needed afterward |
Typical full-deletion flow
terraform workspace select staging
terraform plan -destroy -var-file=staging.tfvars -out=tfplan
terraform show tfplan
terraform destroy -auto-approve -var-file=staging.tfvarsThe lifecycle prevent_destroy attribute blocks deletion of the target resource at the plan stage. It is the last line of defense protecting must-protect resources (KMS keys, shared VPCs, state-backing S3, etc.) from mistakes. It cannot be bypassed by -target or -auto-approve. Removing it requires a code change and a re-plan.
create_before_destroy reduces downtime during replacement. It is not about the safety of deletion itself, but is used as an option to improve availability during replacement. Combine it with cloud-side deletion protection (e.g., S3 bucket deletion protection, RDS deletion protection, IAM policies).
Basic protection patterns (example: protecting a critical bucket)
resource "aws_s3_bucket" "state" {
bucket = "org-terraform-state"
lifecycle {
prevent_destroy = true
}
}
# Example prioritizing availability during replacement
resource "aws_lb" "app" {
# ...
lifecycle {
create_before_destroy = true
}
}Workspaces isolate state per environment. Always confirm the current workspace before destroy, and use environment-specific variable files to make configuration differences explicit. With a remote backend, locking is enabled by default, preventing concurrent runs.
In CI/CD, avoid routine use of -auto-approve and put approval gates in place as the default. When API limits or dependencies cause runs to take time, adjust the wait time with -lock-timeout.
Combining workspaces and variables
terraform workspace list
terraform workspace select prod
terraform plan -destroy -var-file=env/prod.tfvars -lock-timeout=5m -out=tfplan
terraform destroy -var-file=env/prod.tfvarsDependency errors stem from upstream resources still remaining, circular references, or provider limits. Break the plan down to destroy dependents first, or disconnect them safely by hand and then re-run. Use -target minimally and only for this staged tear-down.
Drift (divergence between state and reality) comes from external changes or past state operations. First visualize the diff with plan; if needed, restore consistency with import before running destroy. If you hit API rate limits, lower -parallelism to give retries some breathing room.
Common recovery commands
# Target the dependent resource first and delete in stages
terraform destroy -target=aws_route_table_association.app
# Resolve drift, then delete
terraform import aws_iam_role.app my-role-arn
terraform plan -destroy
# Relieve rate limiting
terraform destroy -parallelism=2Associate
問題 1
In a development environment, you want to safely delete only a specific Lambda function while keeping the API Gateway. Which is the most appropriate procedure?
正解: A
Partial deletion uses -target as an exception. First visualize the impact with plan -destroy -target, confirm there are no issues, and then run a targeted destroy. state rm does not perform a physical deletion, and manual deletion via the console causes drift. Full deletion does not meet the requirement.
What scope of dependencies does -target include?
Dependencies required by the specified resource may be included in the plan, but dependents (upstream resources that depend on it) are not. So for partial deletion, you must verify that breaking the dependents temporarily is acceptable for your design.
What is the difference between terraform plan -destroy and terraform destroy?
plan -destroy only creates a deletion plan and makes no changes to your infrastructure. destroy actually applies the deletion after confirmation. For safe operations, always proceed in the order plan -destroy → review → destroy.
Can you bypass a failure caused by lifecycle.prevent_destroy?
You cannot bypass it. prevent_destroy blocks at the planning phase, so even -auto-approve or -target cannot get past it. If deletion is required, temporarily remove the setting from your code, review it, then re-plan and apply.
Practice with certification-focused question sets
無料で問題を解いてみるNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
HCL Syntax: Terraform's Configuration Language (2026)
HCL2 fundamentals for Terraform — blocks, attributes, expres...
Terraform Authoring & Operations Pro: Complete Guide (2026)
Tactics for the Terraform Pro exam — module authoring, works...
Terraform Providers: Plugin Management Fundamentals (2026)
Provider mechanics — required_providers, versions, mirrors, ...
Terraform Resource Blocks: Declarative Infra Units (2026)
Resource block fundamentals — addresses, references, common ...
Terraform Data Sources: Read-Only External Data (2026)
Data source basics — declaration, refresh behavior, dependen...