{"library":"serverless-s3-cleaner","title":"Serverless S3 Cleaner Plugin","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install serverless-s3-cleaner"],"cli":null},"imports":["plugins:\n  - serverless-s3-cleaner","custom:\n  serverless-s3-cleaner:\n    # plugin specific options","custom:\n  serverless-s3-cleaner:\n    buckets:\n      - your-bucket-name-1\n      - your-bucket-name-2","custom:\n  serverless-s3-cleaner:\n    bucketsToCleanOnDeploy:\n      - old-bucket-to-empty-before-deploy"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"service: my-s3-cleanup-app\n\nprovider:\n  name: aws\n  runtime: nodejs20.x # Or your preferred runtime\n  region: us-east-1\n  # Ensure the IAM role used by Serverless for deployment has necessary S3 permissions.\n  # This is a critical step to prevent deployment/removal failures.\n  iam:\n    role:\n      statements:\n        - Effect: \"Allow\"\n          Action:\n            - s3:ListBucket\n            - s3:ListBucketVersions\n            - s3:DeleteObject\n            - s3:DeleteObjectVersion\n          Resource:\n            - \"arn:aws:s3:::my-unique-data-bucket-${sls:stage}\"\n            - \"arn:aws:s3:::my-unique-data-bucket-${sls:stage}/*\"\n            - \"arn:aws:s3:::static-assets-old-name-${sls:stage}\"\n            - \"arn:aws:s3:::static-assets-old-name-${sls:stage}/*\"\n\nplugins:\n  - serverless-s3-cleaner\n\ncustom:\n  serverless-s3-cleaner:\n    # Set to 'true' to require confirmation before any bucket emptying operation.\n    # Highly recommended for production environments to prevent accidental data loss.\n    prompt: false\n\n    # Buckets listed here will be emptied when you run 'sls remove' for this stack.\n    buckets:\n      - my-unique-data-bucket-${sls:stage} # Example: my-unique-data-bucket-dev\n\n    # Buckets listed here will be emptied BEFORE 'sls deploy' runs for this stack.\n    # Useful for cleaning up old buckets when you've renamed them in your stack definition.\n    bucketsToCleanOnDeploy:\n      - static-assets-old-name-${sls:stage} # This bucket will be cleaned on deploy.\n\nresources:\n  Resources:\n    MyDataBucket:\n      Type: AWS::S3::Bucket\n      Properties:\n        BucketName: my-unique-data-bucket-${sls:stage}\n        VersioningConfiguration:\n          Status: Enabled # Versioned buckets are fully supported by this plugin.\n    OldStaticAssetsBucket:\n      Type: AWS::S3::Bucket\n      Properties:\n        BucketName: static-assets-old-name-${sls:stage}\n\nfunctions:\n  exampleFunction:\n    handler: handler.hello\n    events:\n      - httpApi:\n          path: /hello\n          method: get","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}