Not sure if this is valid requirement, is there a way to destroy a resource, e.g. an instance, by TF file with terraform apply command?
Hi @snowsky,
To fully understand your need, do you require the apply command, or doing something like:
terraform destroy -target RESOURCE_TYPE.NAME
Would be ok?
@Ninir yes, it is. Is it an existing function?
Yes, terraform destroy is a command that allows you to destroy either a full stack (based on your TF files), or single resources, using the -target
option. You can even do:
terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME
Does it solve your problem?
Yes, great to know, Thanks @Ninir
@Ninir , do we have an option of skipping a resource while destroying terraform - by giving its name ?
I understand we can give all the resources name which we want to destroy and skip the one which we want (i believe ).
Won't it be great if we have such an option.
@Pradeep5592 like an --exclude option?
I also use the following options when I destroy several resources.
terraform destroy -target=RESOURCE_TYPE.NAME -target=RESOURCE_TYPE2.NAME
But, someone told me that 'terraform apply' is better than 'terraform destroy' without using 'target' option.
I was wondering whether anyone heard about it or whether you have any experience between these two ways to destroy resources when you create lots of instances.
@shawnhankim To my understanding, the main difference between "removing resources + running apply" and "running destroy" is that in the former, the intent is to permanently remove those resources from your infrastructure (since you're removing it from your IaC codebase altogether), while the latter is useful for when you intend on keeping that infrastructure but want to temporarily destroy it (e.g., to do a "fresh" creation, have provisioners run, etc).
@Pradeep5592 @snowsky and others searching for an exclude flag, it seems like this is something in the works.
How do you remove resources with terraform apply vs terraform destroy?
@ura718 I am pretty sure it involves removing or reducing the count of a resource from a tf file eg count = 2 from previous value of 3, terraform apply dutifully destroys one of the resources (the most recent IIRC).
Doesn't a targeted terraform destroy defeat the whole purpose of infrastructure as code?
If you need to destroy a resource, either delete it from the tf codebase or put it under a count directive and set that count to 0.
This sounds more in line with the way infra as code should be managed.
Obviously the existence of a targeted destroy command means there's a legitimate use for it :-)
I'm wondering what others think about this.
i have created 2 ec2 instance by providing a count 2 as variable,, now i only want to delete the the 2nd node,, if i do "terraform destroy -target=aws_instance.jumpserver[1]"
it also removed the ebs volume attached to the 1st along with 2nd
```Terraform will perform the following actions:
aws_ebs_volume.vol_generic_data[0]
aws_ebs_volume.vol_generic_data[1]
aws_instance.jumpserver[1]
aws_volume_attachment.generic_data_vol_att[0]
aws_volume_attachment.generic_data_vol_att[1]
````
Yes, terraform destroy is a command that allows you to destroy either a full stack (based on your TF files), or single resources, using the
-target
option. You can even do:terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME
Does it solve your problem?
Can any resource (single resource) be destroyed by using Terraform Enterprise version. Is there any possibility to do it in Terraform enterprise.
i want to destroy resources,simply by hitting terraform -destroy, part of this i need to clean up, manually created dns record, for this i need to know the machine name or ip address. how to get this.
provisioner "local-exec" {
when = "destroy"
Yes, terraform destroy is a command that allows you to destroy either a full stack (based on your TF files), or single resources, using the
-target
option. You can even do:terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME
Does it solve your problem?
What does resource type mean?
@ReddyMalathi
resource "resource_type" "resource_name" {
...
}
When you define a resource block the first quoted part is the resource type and the second part is the name. See documentation examples here.
Yes, terraform destroy is a command that allows you to destroy either a full stack (based on your TF files), or single resources, using the
-target
option. You can even do:terraform destroy -target RESOURCE_TYPE.NAME -target RESOURCE_TYPE2.NAME
Does it solve your problem?
What does resource type mean?
You may also list all your resources with terraform state list
.
I also use the following options when I destroy several resources.
terraform destroy -target=RESOURCE_TYPE.NAME -target=RESOURCE_TYPE2.NAMEBut, someone told me that 'terraform apply' is better than 'terraform destroy' without using 'target' option.
I was wondering whether anyone heard about it or whether you have any experience between these two ways to destroy resources when you create lots of instances.
That's a good point, I personally feel, as a best practice if you don't need some resource just remove it from your TF config and then apply the new plan. It will check against the state and remove that resource.
I also use the following options when I destroy several resources.
terraform destroy -target=RESOURCE_TYPE.NAME -target=RESOURCE_TYPE2.NAMEBut, someone told me that 'terraform apply' is better than 'terraform destroy' without using 'target' option.
I was wondering whether anyone heard about it or whether you have any experience between these two ways to destroy resources when you create lots of instances.
I'm trying to delete selected resources (eg. such as aws_subnet
) without deleting the linked aws_eip
so that the eip can be preserved. but it seems like there is no way to control terraform trying to delete all associated resources, even when naming each -target
separately
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Most helpful comment
Yes, terraform destroy is a command that allows you to destroy either a full stack (based on your TF files), or single resources, using the
-target
option. You can even do:Does it solve your problem?