Do you need to delete many S3 objects under a certain prefix, but you don’t want or can use S3 batch jobs? Here you go again, blazingly fast.

import logging

import boto3

logger = logging.getLogger("")

# AWS credentials
AWS_ACCESS_KEY = "your-aws-access-key"
AWS_SECRET_KEY = "your-aws-secret-key"

def batch_remove_s3_objects(bucket: str, prefix: str) -> None:
    Removes all objects from the destination bucket which reside 
    under the given prefix.

    :param bucket S3 bucket name
    :param prefix Object prefix path
        "Truncating objects in bucket %s under prefix %s" % (bucket, prefix)

    session = boto3.Session(
    s3 = session.resource("s3")
    bucket = s3.Bucket(bucket)

if __name__ == "__main__":
    batch_remove_s3_objects("bucket-name", "prefix")