Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save taqiabdulaziz/0e86a491b5d684dfb1f203dd5ed59f65 to your computer and use it in GitHub Desktop.
Save taqiabdulaziz/0e86a491b5d684dfb1f203dd5ed59f65 to your computer and use it in GitHub Desktop.
terraform remove all state
#!/bin/bash
# Run terraform state list command and store output in a variable
TF_STATE_LIST=$(terraform state list)
# Check if the TF_STATE_LIST variable is empty
if [ -z "$TF_STATE_LIST" ]; then
echo "No resources to remove from state."
exit 0
fi
# Iterate over each line of the terraform state list output
while IFS= read -r RESOURCE; do
echo "Removing resource from state: $RESOURCE"
# Run terraform state rm for each resource
terraform state rm "$RESOURCE"
done <<< "$TF_STATE_LIST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment