Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
navi86 opened this issue Oct 8, 2019 · 8 comments

Comments

@navi86
Copy link

navi86 commented Oct 8, 2019

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

@navi86 navi86 changed the title command: terraform state rm require specific resources command: terraform state rm requires specific resources Oct 8, 2019
@hashibot hashibot added the cli label Oct 8, 2019
@teamterraform teamterraform changed the title command: terraform state rm requires specific resources feature request: terraform state rm option to remove all resources created with a given provider Oct 8, 2019
@ghost
Copy link

ghost commented Dec 16, 2019

terraform state list | xargs -L 1 terraform state rm

@remoe
Copy link

remoe commented Jan 1, 2020

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

@dmccaffery
Copy link

or, without locking state between each resource:

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

@sann05
Copy link

sann05 commented Sep 29, 2020

also worked for me:

terraform state rm $(terraform state list | grep aws_instance)

@arash-bizcover
Copy link

On terragrunt, this worked for me

terragrunt state list | sed 's/"/\\"/g' | xargs -I {} terragrunt state rm {}

@erpel
Copy link

erpel commented Oct 9, 2023

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
Copy link

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

@yurinogueira
Copy link

Just use

terraform state rm $(terraform state list)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants