How to delete a Dynamodb Table on CDK destroy

avatar

Borislav Hadzhiev

Fri Apr 23 20211 min read

Updated on Fri Apr 23 2021

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 #

Add me on LinkedIn

I'm a Web Developer with TypeScript, React.js, Node.js and AWS experience.

Let's connect on LinkedIn

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee