Terraform
Destroy a resource
You can use the following methods to destroy resources that you no longer need:
- Remove a resource from the configuration. When you apply the configuration, Terraform compares the configuration with the existing state and instructs the cloud provider to destroy the real infrastructure resource that no longer exists in the configuration.
- Use the
terraform destroycommand to destroy all resources, or use the-targetflag to destroy specific resources. - Replace the resource configuration with a
removedblock and add a destroy-time provisioner to perform additional operations, such as printing custom messages, when Terraform destroys the resource.
Refer to the following topics for more information:
- Remove a resource from state provides instructions on how to remove a resource from state without destroying it.
- Refactor modules provides instructions on refactoring reusable modules.
Destroy a single resource
- Delete the
resourceblock from your configuration. - Delete any references to the resource's attributes. You can run the
terraform validatecommand to find references in your configuration. - Run a
terraform applycommand.
Terraform destroys the actual infrastructure resource and removes it from the state.
Destroy all infrastructure
To destroy all infrastructure managed by Terraform in the current working directory, run the terraform destroy command. Refer to the terraform destroy command reference for more information.
The terraform destroy command does not update the Terraform configuration. As a result, you can run terraform apply to create new instances of the destroyed infrastructure.
Configure destroy-time operations
You can configure Terraform to perform additional operations when destroying a resource by replacing the resource block in your configuration with a removed block and adding a destroy-time provisioner. Refer to the example in the removed block reference for example code.