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("your.logger.name")

# 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
    """
    logger.info(
        "Truncating objects in bucket %s under prefix %s" % (bucket, prefix)
    )

    session = boto3.Session(
        aws_access_key_id=AWS_ACCESS_KEY,
        aws_secret_access_key=AWS_SECRET_KEY,
    )
    s3 = session.resource("s3")
    bucket = s3.Bucket(bucket)
    bucket.objects.filter(Prefix=prefix).delete()


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