How to Delete a Specific Resource in Terraform

0 min read 107 words

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.

Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags