How to delete a Dynamodb Table on CDK destroy


Borislav Hadzhiev

Last updated: Apr 13, 2022


Check out my new book

Deleting Dynamodb Tables on CDK Destroy #

In order to delete a Dynamodb table on CDK destroy we need to override the table's removalPolicy property and set it to DESTROY.

const table = new dynamodb.Table(this, 'my-table', { // 👇 set RemovalPolicy to DESTROY removalPolicy: cdk.RemovalPolicy.DESTROY, partitionKey: {name: 'date', type: dynamodb.AttributeType.STRING}, });

If we were to run cdk destroy after we've updated the table's removalPolicy, our table would get deleted along with the stack.

Default Behavior is to Retain Stateful Resources #

By default when we provision a Dynamodb table using AWS CDK its removalPolicy property is set to RETAIN - docs.

const table = new dynamodb.Table(this, 'my-table', { removalPolicy: cdk.RemovalPolicy.RETAIN, partitionKey: {name: 'date', type: dynamodb.AttributeType.STRING}, });

The same counts for any stateful resources we provision, i.e. s3 buckets.

The default behavior is that the dynamodb table will not get deleted, instead it will remain in our account as an orphaned resource from the stack.

The same counts in the case that we delete the Dynamodb table resource from the stack, it will still remain in our account.

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.