Skip to content

Instantly share code, notes, and snippets.

@ameir
Created February 2, 2021 18:16
Show Gist options
  • Save ameir/15d5721ccef43448b27c23a0eb192655 to your computer and use it in GitHub Desktop.
Save ameir/15d5721ccef43448b27c23a0eb192655 to your computer and use it in GitHub Desktop.
Delete CloudFormation stacks based on prefix or regex
#!/usr/bin/env bash
set -euo pipefail
export AWS_PAGER=""
REGEX="^prefix-"
WAIT=true
stacks=$(aws cloudformation describe-stacks --query Stacks[].StackName --output=text)
for stack in $stacks; do
if [[ $stack =~ $REGEX ]]; then
echo "Deleting stack ${stack}..."
aws cloudformation delete-stack --stack-name $stack
if $WAIT; then
echo "Waiting for stack ${stack} to delete..."
aws cloudformation wait stack-delete-complete --stack-name $stack
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment