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
#...

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

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