Created
March 23, 2021 01:29
-
-
Save ImIOImI/8e9700f64df5c799162d4bab166ca524 to your computer and use it in GitHub Desktop.
Delete Terraform State By Simple Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[Parameter(Mandatory=$true)][string]$Pattern = "*" | |
) | |
$currentState = terraform state list | |
foreach($line in $currentState) { | |
if ($line -like $Pattern) { | |
terraform state rm $line | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.