Delete bucket items

Delete an item in an S3 bucket

The following function can be used to delete an item in an S3 bucket

import boto3

def delete_item(key, bucket):
    # delete item from s3 bucket
    client = boto3.client('s3')
    client.delete_object(
        Bucket=bucket, 
        Key=key
    )

When calling the function you need to pass

  • key - the key ("filepath") of the item you want to remove from the S3 bucket

  • bucket - the name of the bucket

key = 'key_name'
bucket = 'bucket_name'

# delete bucket item
delete_item(key=key, bucket=bucket)

Last updated

Was this helpful?