How to Delete a Stack in AWS CloudFormation Quickly
To delete a stack in AWS CloudFormation, use the
DeleteStack action via the AWS Management Console, AWS CLI, or SDK. This removes all resources created by the stack and deletes the stack itself.Syntax
The DeleteStack operation removes a CloudFormation stack and all its resources. You can delete a stack using the AWS CLI command or SDK method.
- AWS CLI: Use
aws cloudformation delete-stack --stack-name <stack-name> - SDK (example in Python boto3): Call
client.delete_stack(StackName='stack-name')
Replace <stack-name> with your stack's name or ID.
bash
aws cloudformation delete-stack --stack-name my-sample-stack
Example
This example shows how to delete a stack named my-sample-stack using the AWS CLI. It will start the deletion process and remove all resources created by the stack.
bash
aws cloudformation delete-stack --stack-name my-sample-stack
Output
No output if successful; the stack deletion starts asynchronously.
Common Pitfalls
Common mistakes when deleting stacks include:
- Trying to delete a stack with resources that have termination protection enabled. You must disable termination protection first.
- Deleting stacks with resources that are in use or locked by other services, causing deletion to fail.
- Not waiting for the stack deletion to complete before recreating a stack with the same name.
Always check the stack events in the console or CLI to see if deletion failed and why.
bash
aws cloudformation update-termination-protection --stack-name my-sample-stack --no-enable-termination-protection aws cloudformation delete-stack --stack-name my-sample-stack
Quick Reference
Tips for deleting CloudFormation stacks:
- Use the AWS Management Console for a visual way to delete stacks.
- Use the AWS CLI for scripting and automation.
- Check stack events for errors during deletion.
- Disable termination protection before deleting.
- Deletion is asynchronous; monitor progress.
Key Takeaways
Use
aws cloudformation delete-stack --stack-name <stack-name> to delete a stack via CLI.Disable termination protection before deleting a stack to avoid errors.
Stack deletion removes all resources created by the stack.
Deletion is asynchronous; monitor stack events for progress and errors.
You can delete stacks via AWS Console, CLI, or SDK depending on your workflow.