Javatpoint Logo
Javatpoint Logo

Terraform Destroy Command

Terraform is an open-source infrastructure as code (IaC) tool. It enables users to manage infrastructure resources declaratively. It allows the user to define, provision, and operate cloud resources across multiple providers using a single configuration file. One of the most important commands in Terraform is "terraform destroy." This command is used to delete all the resources created using the Terraform configuration. In this article, we are going to learn briefly the Terraform destroy command, how it works, and some examples to help you understand its usage.

What is Terraform Destroy Command?

Terraform destroy command is used to remove all the resources that were created using the Terraform configuration. It is the opposite of the "terraform apply" command. The terraform apply command is used to create resources. The destroy command removes all the resources specified in the configuration files and will prompt the user for confirmation before deleting the resources.

When to Use Terraform Destroy Command?

There is some scenario where we can use terraform destroy command. These are as follows.

  1. When the user wants to delete specific resources that are no longer required or have been replaced by new ones, we use destroy command.
  2. When the user wants to destroy the entire infrastructure stack, including the networking, computing, and storage resources, then we use destroy command.
  3. When the user wants to delete all the resources created using Terraform, we use destroy command to clean up the environment or start over with a new configuration.

How does Terraform Destroy Command Work?

There are some ways by which we can implement the terraform destroy command. These ways are as follows.

  1. First, the terraform reads the Terraform configuration files to determine the resources that need to be deleted.
  2. Then it prompts the user for confirmation before deleting the resources.
  3. After the user's confirmation, it deletes the resources in the reverse order of their creation so that resources depending on other resources are deleted last.
  4. After deleting the resources, It updates the Terraform state file to remove the deleted resources from the state.

Example of Terraform Destroy Command

Let's take an example to understand briefly the terraform destroy command.

1. First, we have to create a new directory, and then we have to navigate to that directory. This can be done by the below commands.

2. Then we have to create a new terraform configuration file and name it "main.tf". Then we have to write the below code in the "main.tf" file.

This code creates an AWS EC2 instance with the help of specified AMI and instance type.

3. Then we have to initialize the working directory with the help of the below commands.

4. Then we have to create the resources with the help of the below commands.

5. Then we have to verify the instances that were created previously. That can be done by the below commands.

6. Then we have to delete the resources with the help of the below commands.

Then a confirmation is prompted for the user before deleting the resource. When the user approves it at that moment, the resources are successfully destroyed.

Advantages of Terraforming Destroy Command

There are some advantages of terraforming destroy command. These are as below.

  1. Easy cleanup: The Terraform destroy command makes it easy to clean up the resources that were created using Terraform. This helps to avoid cluttering your infrastructure with unused resources.
  2. Efficiency: The Terraform destroy command removes all the resources in one go without leaving any errors. This saves time and ensures that resources are removed efficiently.
  3. Cost savings: Removing unused resources can save you money on cloud provider bills. The Terraform destroy command helps you to identify and remove these resources.
  4. Consistency: The Terraform destroy command helps to maintain consistency in your infrastructure by removing all resources that were created by Terraform.

Disadvantages of Terraforming Destroy Command

There are some disadvantages of terraforming destroy command. These are as below.

  1. Accidental destruction: The Terraform destroy command can lead to the accidental destruction of resources if used incorrectly. It'sIt's important to use this command with caution and double-check the resources that will be destroyed.
  2. Data loss: If the user is not careful, then the user can lose data by accidentally destroying resources that contain important data. It'sIt's important to make backups of data before using the Terraform destroy command.
  3. Time-consuming: Destroying resources can take a long time, depending on the number and complexity of the resources. This can be an inconvenience when the user needs to quickly clean up resources.
  4. Dependency issues: The Terraform destroy command can have dependency issues, where a resource cannot be destroyed because it'sit's dependent on another resource. This can cause problems with cleanup and may require additional work to resolve.

Conclusion

The Terraform destroy command is an essential tool that allows the user to delete resources created by Terraform configuration files. It helps to clean up the environment and start over with a new configuration. In this article, we have discussed the usage of the Terraform destroys command and how it works and provided an example to illustrate its usage.


Next TopicTerraform For Loop





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA