How to Delete a CDK Stack in AWS CDK
If you've been playing around with AWS CDK and now need to clean up by deleting a stack, you're in the right place.
Deleting a CDK stack is pretty straightforward. Let's walk through the steps together.
I'll assume you have the following:
- AWS CLI installed on your machine.
- Your AWS credentials are configured.
Go to Your CDK Project
In your terminal, using the cd
command, move into the directory where your CDK project is located. This is the folder with your cdk.json
file.
List Your Stacks
Before deleting, it's a good idea to see your stacks. Type in:
cdk list
or
npx aws-cdk list
This command shows all the stacks in your app. Find the name of the stack you want to delete.
Delete the Stack
Now, to delete the stack, use the following command:
cdk destroy <YOUR_STACK_NAME>
or
npx aws-cdk destroy <YOUR_STACK_NAME>
Replace <YOUR_STACK_NAME>
with the name of the stack you want to get rid of. For example, if your stack's name is MyStack
, you would type:
cdk destroy MyStack
The command will ask for confirmation that you really want to delete the stack. Type y
and press Enter to confirm.
Gotchya: Resources That Don't Get Deleted by Default
One important thing to remember is that not all resources are deleted automatically when you run the cdk destroy
command.
AWS CDK tries to be careful not to remove data or resources that could be critical or hard to replace.
Here's a bit more on that since it's a headache I've ran into:
Persistent Resources
Certain resources, like Amazon S3 buckets that are not empty or DynamoDB tables, are designed to be persistent. That means they won’t be deleted by default because AWS wants to prevent accidental data loss.
This is super important if you have critical data stored there!
Custom Deletion Policies
In some cases, resources in your CDK stack might have custom deletion policies. These policies can specify that a resource should be retained (not deleted) when the stack is destroyed. This is another safety feature to prevent losing important stuff.
So, after you run cdk destroy
, it's a good move to:
- Check the AWS Management Console: Go to the relevant service pages (like S3 or DynamoDB) and see if there are any leftovers.
- Manual Cleanup: If you find resources that weren't deleted, you might need to delete them manually. Just make sure you really don’t need them anymore before you hit that delete button.
- Alter the Deletion Policy: If you are sure that you'd like the resources easy to clean up, you can usually modify the
removalPolicy
property toDESTROY
, which means that if the resource is empty when we delete the CDK stack, the resource will also get deleted. In something like DynamoDB, that would look like this:
const dbTable = new dynamodb.Table(this, "database-table", { partitionKey: { name: "someKey", type: dynamodb.AttributeType.NUMBER }, billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, // Set a removal policy of DESTROY removalPolicy: cdk.RemovalPolicy.DESTROY, });
Regardless, after the stack is deleted, it's a good idea to double-check your AWS Management Console to make sure everything related to it is really gone. I've often found that some resources might need to be deleted manually.
And that's it!
You've successfully deleted a CDK stack.
If you need to recreate the stack, run your CDK deploy command again.