How to destroy resources through Terraform?

In 3 ways we can destroy the resources through Terrafrom.

1.From the terrafrom init directroy – Where you keep the actual config files,
2.Specifying the target with provider,
3.Remove the resource code from your tf file.

Option1: – From the terraform init directory

#terraform destroy

Here it will show clearly what are the resources can be deleted . In this example “yourec2” will be deleted . We didn’t specify the actual target resources here . But it will read form available tf config files and shows.

2. Now lets try to delete the below instance by specifying the target

#terraform destroy -target aws_instance.yourec2

3.Remove the code from your tf file

In thes example we have comment the resource part , so it will terminate the ec2 instance . If it is already terminated it will show no changes.

It makes other resources available and confirm ec2 instance not exists.


Leave a Reply