how to delete files from amazon s3 bucket?

Suhail picture Suhail · Jun 29, 2010 · Viewed 117k times · Source

I need to write code in python that will delete the required file from an Amazon s3 bucket. I am able to connect to the Amazon s3 bucket, and also to save files, but how can I delete a file?

Answer

Kohányi Róbert picture Kohányi Róbert · May 10, 2017

Using boto3 (currently version 1.4.4) use S3.Object.delete().

import boto3

s3 = boto3.resource('s3')
s3.Object('your-bucket', 'your-key').delete()