HashiCorp Terraform Associate (004) Question 76
Single answer3f Destroy Terraform-managed infrastructureA team uses Terraform to manage an AWS-based development environment. The configuration in the current working directory still matches the deployed resources, and the remote state is healthy. Before a major redesign, the team wants to remove all infrastructure managed by this configuration while keeping the Terraform code in version control for future reuse. Which action should they take?
- A
Run terraform destroy from the working directory and review the proposed deletions before approving
- B
Delete the terraform.tfstate file, then run terraform apply so Terraform recreates only missing resources
- C
Remove all resource blocks from the configuration and run terraform apply to force Terraform to forget the infrastructure
- D
Run terraform plan -destroy only, because it both previews and executes the destruction of all managed resources
Show answer and explanation
Correct answer: A
Explanation
When the goal is to remove all infrastructure currently managed by a Terraform configuration, the correct command is terraform destroy. This command is effectively a convenience workflow for creating and applying a destroy plan for all managed resources in the current state. It is appropriate when the configuration should be retained but the deployed environment should be torn down. By contrast, deleting the state file only discards Terraform's tracking information and does not affect real infrastructure. Removing resource blocks and applying can also lead to destruction, but it changes the desired configuration and is not the recommended approach when the team intends to preserve the codebase for future redeployment. HashiCorp documentation describes terraform destroy as the primary command for destroying managed infrastructure, and terraform plan -destroy as a way to preview that operation before execution.
- A. Correct.
Correct.
terraform destroyis the standard workflow to destroy all remote objects managed by a particular Terraform configuration. It creates a destroy plan and prompts for approval before execution, allowing the team to review what will be deleted. This matches the requirement to remove infrastructure while keeping the code for future reuse. - B. Incorrect.
Incorrect. Deleting the state file does not destroy infrastructure. It only removes Terraform's record of managed objects, causing resources to become unmanaged from Terraform's perspective. This is dangerous because the infrastructure would still exist, and future operations could lead to drift, duplication, or import requirements.
- C. Incorrect.
Incorrect. If you remove resource blocks and run
terraform apply, Terraform will plan to destroy resources that are no longer declared, but this is not the best action for this scenario because the team explicitly wants to keep the configuration in version control for reuse. Removing the blocks changes the desired configuration rather than intentionally destroying the current environment. It also increases the risk of accidental code loss or incomplete teardown if not all resources are removed. - D. Incorrect.
Incorrect.
terraform plan -destroygenerates and shows a destroy execution plan, but it does not perform the destruction. It is useful for previewing a full teardown, but a separateterraform destroyorterraform applyof a saved destroy plan is required to actually remove the infrastructure.