Search code examples
aws-cloudformationaws-serverlessinfrastructure-as-code

Delete all changeset on CloudFormation stack without deleting stack


Sometimes you may reach to max count of changesets in CloudFormation. When using AWS CLI or AWS Console, it is possible to delete changesets one by one. However, how can one delete all changesets associated with a stack without removing the stack itself?


Solution

  • On Mac:

    1. Install jq with brew:

      brew install jq

    2. You have to install aws-cli and configure your access key and secret

    3. Create a file like clear-changeset.sh and paste below code into it:

      aws cloudformation list-change-sets --stack-name YOUSTACKNAME > STACK_RESULT.json
      max_processes=4 
      count=0
      
      for item in $(cat STACK_RESULT.json | jq -r '.Summaries[].ChangeSetName'); do
          echo "Deleting change set: $item"
          aws cloudformation delete-change-set --stack-name YOUSTACKNAME --change-set-name "$item" &
          ((count++))
          if ((count >= max_processes)); then
              wait
              count=0 
          fi
      done
      wait  
      
    4. run the clear-changeset.sh file like:

      ./clear-changeset.sh

    Attention:
    Check the .sh file permissions to run it (chmod 0755 clear-changeset.sh).