Automatically delete an S3 bucket with the AWS CDK stack

AWS CDK is the latest Infrastructure as Code tool, made by AWS itself. It makes it super easy to deploy the various pieces of the infrastructure that your application needs. However, it’s having a hard time cleaning up stale S3 buckets when you no longer need them.

The basic mechanism for creating an S3 bucket as part of CDK stack is this:

const bucket = new Bucket(this, 'my-data-bucket');

However, try deleting the CDK stack using cdk destroy later. You will see that the CloudFormation stack is deleted but the S3 bucket remains. Why?

By default, the Construct that comes from the S3 package, has a default prop called removalPolicy: cdk.RemovalPolicy.RETAIN. This makes sense and is a meaningful default for a lot of use cases, where you wouldn’t want important data (e.g. user avatars or file uploads) to disappear with a simple command.

However, even if we try setting removalPolicy: cdk.RemovalPolicy.DESTROY, we see that on stack removal, it fails with an error that says the Bucket is not empty (CloudFormation does not destroy buckets that are not empty).

We have a couple of solutions here, and picking the right one very much depends on your needs:

Option 1: Manually clean the bucket contents before destroying the stack

This is okay for most cases. You could do this from the AWS S3 user interface or through the command line, using the AWS CLI:

# Cleanup bucket contents without removing the bucket itself
aws s3 rm s3://bucket-name --recursive

Then the cdk destroy should proceed without errors. However, this can quickly become a tedious activity if your stacks contain multiple S3 buckets or you use stacks as a temporary resource (e.g. you deploy a CDK stack for every client of your platform programmatically). In any case, some automation would help. Which brings us to the next option.

Option 2: Automatically clear bucket contents and delete the bucket

An interesting third party package called @mobileposse/auto-delete-bucket comes to the rescue. It provides a custom CDK construct that wraps around the standard S3 construct and internally uses the CloudFormation Custom Resources framework, to trigger an automated bucket contents cleanup when a stack destroy is triggered. The usage is pretty trivial.

Install the package:

npm i @mobileposse/auto-delete-bucket

Use the new CDK construct instead of the standard one:

import { AutoDeleteBucket } from '@mobileposse/auto-delete-bucket'

const bucket = new AutoDeleteBucket(this, 'my-data-bucket')


Need AWS CDK consulting? We are here to help. Drop us a line.