hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.7k stars 9.55k forks source link

feature request: terraform state rm option to remove all resources created with a given provider #23023

Open navi86 opened 5 years ago

navi86 commented 5 years ago

Hi there, I think command "terraform rm module.${provider_name}" should remove all resources created with that provider. Terraform v0.12.9

Use-cases

I have some helm chart created using provider helm_release

Attempted Solutions

I can delete resources if I specify full path to resource. example terraform rm module.helm_release.dex

Proposal

"terraform rm module.${provider_name}" should remove all resources for defined provider

ghost commented 4 years ago

terraform state list | xargs -L 1 terraform state rm

remoe commented 4 years ago

terraform state list | cut -f 1 -d '[' | xargs -L 1 terraform state rm

dmccaffery commented 4 years ago

or, without locking state between each resource:

terraform state list | cut -f 1 -d '[' | xargs -L 0 terraform state rm
sann05 commented 4 years ago

also worked for me:

terraform state rm $(terraform state list | grep aws_instance)
arash-bizcover commented 3 years ago

On terragrunt, this worked for me

terragrunt state list | sed 's/"/\\"/g' | xargs -I {} terragrunt state rm {}
erpel commented 1 year ago

If using for_each with values or keys that might contain spaces, apply shell escaping for these too. The -I {} was not needed with macOs 13.5 xargs (from FreeBSD)

terraform state list | sed 's/"/\\"/g;s/ /\\ /g'  | xargs terragrunt state rm

Be careful with all of these examples, as using the output of terraform state list unfiltered will work on all resources.

elliotdickison commented 2 months ago

If like me you're terrified of wrecking everything on accident, state rm has a helpful -dry-run flag

yurinogueira commented 1 day ago

Just use

terraform state rm $(terraform state list)