terraform destroy and plan -destroy: removing everything a configuration manages, reviewing a destroy plan first, and the ways to remove a single object instead.
Study it
Destroying infrastructure and the reverse dependency order
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A developer is finished with a temporary environment and wants to see exactly what would be removed before removing everything the configuration manages. Which commands do this?
Aterraform graph to preview, then terraform fmt
Bterraform validate -destroy to preview, then terraform apply
Cterraform show to preview, then delete terraform.tfstate
Dterraform plan -destroy to preview, then terraform destroy
Show the answer and why
Aterraform graph to preview, then terraform fmt
Incorrect
graph shows dependencies and fmt rewrites files; neither removes anything.
Bterraform validate -destroy to preview, then terraform apply
Incorrect
validate has no destroy mode, and apply without -destroy keeps the resources.
Cterraform show to preview, then delete terraform.tfstate
Incorrect
Deleting state leaves every object running and unmanaged.
Dterraform plan -destroy to preview, then terraform destroy
Correct
plan -destroy shows a speculative destroy plan, and terraform destroy is an alias for apply -destroy that prompts before acting.
Destroy is just a planning mode: preview with plan -destroy, run it with destroy (or apply -destroy).
Before an instance is destroyed, a script must deregister it from an external inventory system. Which Terraform feature runs the script at the right moment?
AA local-exec provisioner with on_failure = continue
BA depends_on argument from the instance to the script file in the repository
CA check block that runs the script during destroy
DA provisioner with when = destroy in the instance's block
Show the answer and why
AA local-exec provisioner with on_failure = continue
Incorrect
on_failure controls failure behavior, not when the provisioner runs.
BA depends_on argument from the instance to the script file in the repository
Incorrect
depends_on orders resources; it does not run scripts.
CA check block that runs the script during destroy
Incorrect
Check blocks assert conditions; they do not run scripts.
DA provisioner with when = destroy in the instance's block
Correct
Destroy-time provisioners run before the resource is destroyed; they must stay in the configuration to run.
when = destroy runs a provisioner just before destruction; use it sparingly.