Terraform Destroy Specific Resource – 2 Methods

Terraform is a powerful tool for infrastructure as code, allowing you to manage your resources in a programmatic way. In some cases, you may need to remove a specific resource from your infrastructure.

In this tutorial, we will learn how to destroy specific resources using Terraform with examples.

We are going to show 2 methods that can be used to destroy specific resources using Terraform. These can be used with any cloud provider, whether AWS, Google Cloud, Microsoft Azure, or Oracle Cloud.

Terraform Destroy Specific Resource

[Terraform] Methods to Destroy Specific Resources

We are going to use 2 ways to destroy specific resources managed by Terraform.

  1. Using “terraform destroy” with the target option
  2. Using “terraform apply” with the target option

First of all, it is essential to understand that Terraform’s “destroy” command is designed to destroy all resources managed by a specific Terraform configuration file.

To destroy a specific resource, you will need to edit your configuration file to remove that resource, and then run the “terraform apply” command to apply the changes or you can also use the “terraform destroy” command directly to destroy all the resources or a specific one using the “target” option as we have explained below with the help of examples

Also check: Terragrunt vs Terraform

Method 1: Using Terraform Destroy Target

Terraform destroy command is generally used to destroy all the resources managed by Terraform. If you run the “terraform destroy” command it will try to delete all the resources managed by the terraform.

But if you want to destroy specific resource, you can use “terraform destroy -target=’resource.name'”.

This will only delete the resource specified by the target option. basically, you are targeting that particular resource with the target option.

Example: Suppose you created an AWS EC2 instance, S3 bucket, a related Roles & policies using Terraform, and now you want to only delete the EC2 instance which uses Ubuntu AMI. In that case, you will just use the target option and will target that specific resource.

Below is the sample code for the terraform destroy -target=’resource.name’ command.

tf_ec2 $ terraform destroy -target='aws_instance.st_web1'
aws_instance.st_web1: Refreshing state... [id=i-0537369c8ace134f8]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  - destroy

Terraform will perform the following actions:

  # aws_instance.st_web1 will be destroyed
  - resource "aws_instance" "st_web1" {
      - ami                                  = "ami-007855ac798b5175e" -> null
      - arn                                  = "arn:aws:ec2:us-east-1:213822468222:instance/i-0537369c8ace134f8" -> null
      - associate_public_ip_address          = true -> null
      - availability_zone                    = "us-east-1b" -> null
      - cpu_core_count                       = 1 -> null
      - cpu_threads_per_core                 = 1 -> null
      - hibernation                          = false -> null
      - id                                   = "i-0537369c8ace134f8" -> null
      - instance_initiated_shutdown_behavior = "stop" -> null
      - instance_state                       = "running" -> null
      - instance_type                        = "t2.micro" -> null

    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

aws_instance.st_web1: Destroying... [id=i-0537369c8ace134f8]
aws_instance.st_web1: Still destroying... [id=i-0537369c8ace134f8, 10s elapsed]
aws_instance.st_web1: Still destroying... [id=i-0537369c8ace134f8, 20s elapsed]
aws_instance.st_web1: Destruction complete after 29s

│ Warning: Applied changes may be incomplete

Destroy complete! Resources: 1 destroyed.
$

Method 2: Using Terraform Apply Target

In the above example, we saw how to destroy specific resources using Terraform destroy command, but we can also use the “terraform apply” command to delete the resource.

To delete a particular resource, you just need to comment or delete the resource-related lines in your terraform file.

Example: In this example, we are just going to destroy the AWS EC2 instance using the “terraform apply -target=’resource.name’“. Before running this command just comment or remove the lines from your terraform file.

tf_ec2 $ terraform apply -target='aws_instance.st_web1'
aws_instance.st_web1: Refreshing state... [id=i-05987721c42b79560]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  - destroy

Terraform will perform the following actions:

  # aws_instance.st_web1 will be destroyed
  # (because aws_instance.st_web1 is not in configuration)
  - resource "aws_instance" "st_web1" {
      - ami                                  = "ami-007855ac798b5175e" -> null
      - arn                                  = "arn:aws:ec2:us-east-1:213822468222:instance/i-05987721c42b79560" -> null
      - associate_public_ip_address          = true -> null
      - availability_zone                    = "us-east-1b" -> null
      - cpu_core_count                       = 1 -> null
      - cpu_threads_per_core                 = 1 -> null
      - id                                   = "i-05987721c42b79560" -> 
      - private_dns                          = "ip-172-31-93-151.ec2.internal" -> null
      - private_ip                           = "172.31.93.151" -> null
...................................
...................................
      - root_block_device {
          - delete_on_termination = true -> null
          - device_name           = "/dev/sda1" -> null
          - encrypted             = false -> null
          - iops                  = 100 -> null
          - tags                  = {} -> null
          - throughput            = 0 -> null
          - volume_id             = "vol-0d6881fe1a383fde7" -> null
          - volume_size           = 8 -> null
          - volume_type           = "gp2" -> null
        }
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_instance.st_web1: Destroying... [id=i-05987721c42b79560]
aws_instance.st_web1: Still destroying... [id=i-05987721c42b79560, 10s elapsed]
aws_instance.st_web1: Still destroying... [id=i-05987721c42b79560, 20s elapsed]
aws_instance.st_web1: Still destroying... [id=i-05987721c42b79560, 30s elapsed]
aws_instance.st_web1: Destruction complete after 30s

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.

These were the 2 methods that you can use to destroy specific resources using Terraform. If you have any questions, or facing any problems while deleting the resources, let us know through your comment.

Conclusion

In conclusion, there are multiple ways by which you can destroy specific resources using Terraform in any cloud environment. But knowing which to use for your environment depends upon you. Mostly for the production environment, it is best to use the “terraform apply” command.

If you are facing any issues or errors with Terraform, please let us know.

Buy me a coffeeBuy me a coffee

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.