Skip to main content

Avatar

Build Status Code Scanning Status Dependencies Status Security Status Published Version

S3Empty

S3Empty is a Python CLI for conveniently emptying an AWS S3 bucket. It handles versioned and non-versioned S3 buckets.

This tool is useful when you want to delete all objects in a bucket before deleting the bucket itself, which is a common situation often resulting in the following error message:

BucketNotEmpty: The bucket you tried to delete is not empty. You must delete all versions in the bucket.

S3Empty console screenshot

Installation

pip3 install s3empty

Usage

Run S3Empty with specified bucket name:

s3empty --bucket-name some-bucket

Run S3Empty with a configuration file containing the bucket names:

s3empty --conf-file path/to/some-conf-file.yaml

By default, S3Empty will raise an error if the bucket does not exist. However, there will be scenarios where the S3 bucket you want to empty does not exist or no longer exists. You can use the --allow-inexisting flag to allow inexisting buckets and S3Empty will display a warning message and exits without raising any error:

s3empty --bucket-name some-bucket --allow-inexisting

Show help guide:

s3empty --help

Configuration

You can specify multiple bucket names in S3Empty configuration file and give it a name with .yaml extension, e.g. some-conf-file.yaml :

---
bucket_names:
  - some-bucket-1
  - some-bucket-2

And then call S3Empty:

s3empty --conf-file path/to/some-conf-file.yaml

The configuration file also supports Jinja template where environment variables are available for use. You can give this configuration template a name with .yaml.j2 extension, e.g. some-conf-file.yaml.j2 .

For example, if there is an environment variable ACCOUNT_ID=1234567 , you can specify it in the configuration file:

---
bucket_names:
  - some-{{ env.ACCOUNT_ID }}-bucket-1
  - some-{{ env.ACCOUNT_ID }}-bucket-2

And then call S3Empty:

s3empty --conf-file path/to/some-conf-file.yaml.j2

Permission

Here's an IAM policy with minimum permissions required by S3Empty:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "S3EmptyPolicy",
            "Effect": "Allow",
            "Action": [
                "s3:GetBucketVersioning",
                "s3:ListBucket",
                "s3:ListBucketVersions",
                "s3:DeleteObject",
                "s3:DeleteObjectVersion",
            ],
            "Resource": [
                "arn:aws:s3:::some-bucket",
                "arn:aws:s3:::some-bucket/*"
            ]
        }
    ]
}

FAQ

Q: How about using S3 Lifecycle to delete objects in the bucket?

A: S3 Lifecycle modifies the state of the S3 bucket by adding the lifecycle configuration. However, S3Empty aims to only modify the S3 objects without modifying the S3 bucket itself. Other than that, S3 Lifecycle has an expiration delay which means, and I quote, "Amazon S3 might not actually delete these objects until days or even weeks later." S3Empty aims to start deleting the objects immediately.

Colophon

Developer's Guide

Build reports:

Metadata

Release files for s3empty 2.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for s3empty 2.1.0
File Size Uploaded Provenance
s3empty-2.1.0.tar.gz 8.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for s3empty 2.1.0
File Interpreter ABI Platform
s3empty-2.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / s3empty-2.1.0.tar.gz

Download URL s3empty-2.1.0.tar.gz
Size 8.6 kB
Tags Source
SHA-256 checksum
How to use checksums
648b369bb6744b7606e57df99396beb58ec704a383134fa2b2cba3a4dfc176fe
BLAKE2b-256 checksum
How to use checksums
2c6e52fb0180820013cf08bbec23d17c0445459aa3aa8307f64865e3898ed690
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.12.13 Linux/6.17.0-1010-azure

Release files / s3empty-2.1.0-py3-none-any.whl

Download URL s3empty-2.1.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4581d1d32a2d9cefaa348e11714aed7a81a0a6618baca3ae2ce599c63f107846
BLAKE2b-256 checksum
How to use checksums
dd99822aef4d3d8c197c3b3d781ee86ca1e58d8842183215582ff632c5371a15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.12.13 Linux/6.17.0-1010-azure

Security for s3empty 2.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page