Serverless S3 Cleaner Plugin

2.0.2 · active · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

service: my-s3-cleanup-app

provider:
  name: aws
  runtime: nodejs20.x # Or your preferred runtime
  region: us-east-1
  # Ensure the IAM role used by Serverless for deployment has necessary S3 permissions.
  # This is a critical step to prevent deployment/removal failures.
  iam:
    role:
      statements:
        - Effect: "Allow"
          Action:
            - s3:ListBucket
            - s3:ListBucketVersions
            - s3:DeleteObject
            - s3:DeleteObjectVersion
          Resource:
            - "arn:aws:s3:::my-unique-data-bucket-${sls:stage}"
            - "arn:aws:s3:::my-unique-data-bucket-${sls:stage}/*"
            - "arn:aws:s3:::static-assets-old-name-${sls:stage}"
            - "arn:aws:s3:::static-assets-old-name-${sls:stage}/*"

plugins:
  - serverless-s3-cleaner

custom:
  serverless-s3-cleaner:
    # Set to 'true' to require confirmation before any bucket emptying operation.
    # Highly recommended for production environments to prevent accidental data loss.
    prompt: false

    # Buckets listed here will be emptied when you run 'sls remove' for this stack.
    buckets:
      - my-unique-data-bucket-${sls:stage} # Example: my-unique-data-bucket-dev

    # Buckets listed here will be emptied BEFORE 'sls deploy' runs for this stack.
    # Useful for cleaning up old buckets when you've renamed them in your stack definition.
    bucketsToCleanOnDeploy:
      - static-assets-old-name-${sls:stage} # This bucket will be cleaned on deploy.

resources:
  Resources:
    MyDataBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: my-unique-data-bucket-${sls:stage}
        VersioningConfiguration:
          Status: Enabled # Versioned buckets are fully supported by this plugin.
    OldStaticAssetsBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: static-assets-old-name-${sls:stage}

functions:
  exampleFunction:
    handler: handler.hello
    events:
      - httpApi:
          path: /hello
          method: get

view raw JSON →