Skip to content

Instantly share code, notes, and snippets.

@ImIOImI
Created March 23, 2021 01:29
Show Gist options
  • Save ImIOImI/8e9700f64df5c799162d4bab166ca524 to your computer and use it in GitHub Desktop.
Save ImIOImI/8e9700f64df5c799162d4bab166ca524 to your computer and use it in GitHub Desktop.
Delete Terraform State By Simple Pattern
param (
[Parameter(Mandatory=$true)][string]$Pattern = "*"
)
$currentState = terraform state list
foreach($line in $currentState) {
if ($line -like $Pattern) {
terraform state rm $line
}
}
@ImIOImI
Copy link
Author

ImIOImI commented Mar 23, 2021

I'm finding I have to use this a lot to clean up some of the messes left behind by k8s cluster provisioning when the cluster is deleted, but Terraform doesn't manage the state files correctly. You can call this like:

terraform-delete-state.ps1 module.eks.data.*

and it will delete everything that matches the prefix module.eks.data.. Obviously, you can put *s anywhere, but this is the most common use case.

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