Skip to content

How to Delete a Specific Resource in Terraform

Running terraform destroy will tear down the whole stack associated to some terraform code.

However, sometimes you might only want to remove a specific piece of your infrastructure.

To do this, you can use the terraform destroy -target object.

Step 1 – List the State

Get a list of all the resources from the state:

terraform state list #data.aws_ami.webserver_ami #aws_autoscaling_group.asg-web[0] #random_string.rand3 #...
Code language: Bash (bash)

Step 2 – Remove a Specific Resource

Run a terraform destroy -target and pass a resource from the state list above:

terraform destroy -target aws_autoscaling_group.asg-web[0] -auto-approve
Code language: Bash (bash)

We also added a -auto-approve in the above command to automatically delete the resource without prompting us for confirmation.

See also  How to Get Last Digit of a Large Number in C
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x