Terraform 101: destroy resources methods

To explore the complete code for this case, visit the repository here.To destroy resources managed by Terraform, you can use the terraform destroy command. This command will destroy all resources that are defined in your Terraform configuration files a…


This content originally appeared on Level Up Coding - Medium and was authored by Jiadong Chen

To explore the complete code for this case, visit the repository here.

To destroy resources managed by Terraform, you can use the terraform destroy command. This command will destroy all resources that are defined in your Terraform configuration files and that have been created by Terraform.

This command is a convenience alias for the terraform apply -destroycommand.

Besides using the command to remove all resources, you can also employ alternative methods to either destroy specific resources or avoid using this command entirely. Let’s explore these options!

Targeted Destruction with terraform destroy

If you want to destroy a specific resource instead of all resources, you can use the -target flag with the terraform destroy command. For example, to destroy the azurerm_storage_account resource from your Terraform file, you would use:

terraform destroy -target=azurerm_storage_account.this

This command will only destroy the specified resource and any resources that depend on it.

Remove the resource definition in the Terraform configuration.

If you remove a resource from your Terraform configuration and then run terraform apply, Terraform will delete the resource.

When you run terraform apply, Terraform compares your configuration with the current state of your infrastructure. If a resource exists in the state but not in the configuration, Terraform interprets this as you wanting to delete the resource, and it will plan to destroy it.

After you confirm the plan, Terraform will delete the resource. This is one way to delete resources managed by Terraform, but be careful to ensure you are removing the correct resource from your configuration.

Here’s the example used here:

terraform {
required_providers {
azurerm = ">= 3.0"
}
}

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "this" {
name = "rg-aue-example-001"
location = "australiaeast"
}

resource "azurerm_storage_account" "this" {
name = "staaueexample001"
resource_group_name = azurerm_resource_group.this.name
location = azurerm_resource_group.this.location
account_tier = "Standard"
account_replication_type = "LRS"
}
terraform destroy   
terraform destroy -target=azurerm_storage_account.this

Thank you for reading! To explore the complete code for this case, visit the repository here. I hope you find it useful!


Terraform 101: destroy resources methods was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Jiadong Chen


Print Share Comment Cite Upload Translate Updates
APA

Jiadong Chen | Sciencx (2024-06-20T14:07:14+00:00) Terraform 101: destroy resources methods. Retrieved from https://www.scien.cx/2024/06/20/terraform-101-destroy-resources-methods/

MLA
" » Terraform 101: destroy resources methods." Jiadong Chen | Sciencx - Thursday June 20, 2024, https://www.scien.cx/2024/06/20/terraform-101-destroy-resources-methods/
HARVARD
Jiadong Chen | Sciencx Thursday June 20, 2024 » Terraform 101: destroy resources methods., viewed ,<https://www.scien.cx/2024/06/20/terraform-101-destroy-resources-methods/>
VANCOUVER
Jiadong Chen | Sciencx - » Terraform 101: destroy resources methods. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/20/terraform-101-destroy-resources-methods/
CHICAGO
" » Terraform 101: destroy resources methods." Jiadong Chen | Sciencx - Accessed . https://www.scien.cx/2024/06/20/terraform-101-destroy-resources-methods/
IEEE
" » Terraform 101: destroy resources methods." Jiadong Chen | Sciencx [Online]. Available: https://www.scien.cx/2024/06/20/terraform-101-destroy-resources-methods/. [Accessed: ]
rf:citation
» Terraform 101: destroy resources methods | Jiadong Chen | Sciencx | https://www.scien.cx/2024/06/20/terraform-101-destroy-resources-methods/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.