serverless-s3-cleaner is a Serverless Framework plugin designed to manage the emptying of AWS S3 buckets as part of the stack deployment and removal lifecycle. It acts as a robust replacement for the unmaintained `serverless-s3-remover` plugin, addressing several limitations including support for emptying versioned buckets and improved console logging. The current stable version is 2.0.2, with releases typically driven by compatibility requirements with new Serverless Framework versions, such as the major update to v2.0.0 for Serverless Framework v3 logging API. Key differentiators include its ability to clean buckets not just on stack removal, but also prior to deployment (useful for renaming buckets), and comprehensive support for S3 bucket versioning, ensuring all object versions and delete markers are purged. It primarily functions through YAML configuration within `serverless.yml` and is tightly integrated with the Serverless Framework's CLI commands.
npm install serverless-s3-cleanerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the `serverless-s3-cleaner` plugin's configuration in `serverless.yml`. It shows how to activate the plugin, define buckets for cleanup on stack removal, and specify buckets to clean before deployment, including essential IAM permissions for S3 operations.
Upgrade Serverless Framework to version 3.x: `npm install -g serverless@latest` or `yarn global add serverless@latest`. Ensure your `serverless.yml` configuration is also compatible with Serverless v3.
Ensure your Serverless provider's IAM role includes the necessary S3 permissions for all buckets listed in your plugin configuration. Refer to the README for exact permissions needed.
For automated CI/CD pipelines, set `prompt: false` in your `serverless.yml` configuration for the `serverless-s3-cleaner` plugin. Example: `custom.serverless-s3-cleaner.prompt: false`.
Ensure Serverless Framework v3.x is used. Update any scripts or tools that might be parsing the raw CLI output of Serverless commands, as logging formats may have changed.
Run `npm install serverless-s3-cleaner --save-dev` and ensure `serverless-s3-cleaner` is listed under the `plugins:` section in your `serverless.yml`.
Add `s3:ListBucket`, `s3:ListBucketVersions`, `s3:DeleteObject`, and `s3:DeleteObjectVersion` permissions to the IAM role that your Serverless stack uses, targeting the specific S3 buckets configured for cleaning.
Verify that the bucket is correctly listed under `custom.serverless-s3-cleaner.buckets` in your `serverless.yml`. Ensure the IAM role has all required S3 permissions and that `prompt: false` is set for non-interactive environments. Rerun `sls remove`.