Delete AWS Cloud formation stack with resources created by it

Viewed 4225

Based on this page I can do:

aws cloudformation delete-stack \
    --stack-name my-stack

It says I can attach the command: [--retain-resources <value>]

Does that mean that if I don't specify that line, all the resources created by the stack will be removed? I'm trying to delete everything generated by the stack, which is a lot.

How can I achieve this?

Thanks

2 Answers

Yes, those resources will be kept if you specify the [--retain-resources <value>], if you dont Cloudformation will delete all the resources in the stack name (including the nested stacks as well) you are providing given you have permissions to do. If any of the resources inside the cloudformation stack has retain policy set they won't be deleted.

During deletion, AWS CloudFormation deletes the stack but does not delete the retained resources.

From the same page aws cloudformation delete-stack

You might wanna read this too How do I retain some of my resources when I delete an AWS CloudFormation stack?

You can specify the retain policy as well in cloudformation template.

In vase if you want to delete all the CFN stacks created in the account, I write small bash script to provide that:

#!/bin/bash
STACKS=$(aws cloudformation list-stacks --stack-status-filter CREATE_IN_PROGRESS CREATE_COMPLETE ROLLBACK_IN_PROGRESS ROLLBACK_FAILED ROLLBACK_COMPLETE DELETE_IN_PROGRESS DELETE_FAILED UPDATE_IN_PROGRESS UPDATE_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_COMPLETE UPDATE_ROLLBACK_IN_PROGRESS UPDATE_ROLLBACK_FAILED UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS UPDATE_ROLLBACK_COMPLETE REVIEW_IN_PROGRESS --query "StackSummaries[*].StackName" --output text)

for stack in $STACKS
do
    aws cloudformation delete-stack --stack-name $stack
done
Related