how to delete files from amazon s3 bucket?
174,043
Solution 1
Using boto3
(currently version 1.4.4) use S3.Object.delete()
.
import boto3
s3 = boto3.resource('s3')
s3.Object('your-bucket', 'your-key').delete()
Solution 2
Using the Python boto3 SDK (and assuming credentials are setup for AWS), the following will delete a specified object in a bucket:
import boto3
client = boto3.client('s3')
client.delete_object(Bucket='mybucketname', Key='myfile.whatever')
Solution 3
found one more way to do it using the boto:
from boto.s3.connection import S3Connection, Bucket, Key
conn = S3Connection(AWS_ACCESS_KEY, AWS_SECERET_KEY)
b = Bucket(conn, S3_BUCKET_NAME)
k = Key(b)
k.key = 'images/my-images/'+filename
b.delete_key(k)
Solution 4
Welcome to 2020 here is the answer in Python/Django:
from django.conf import settings
import boto3
s3 = boto3.client('s3')
s3.delete_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=f"media/{item.file.name}")
Took me far too long to find the answer and it was as simple as this.
Solution 5
please try this code
import boto3
s3 = boto3.client('s3')
s3.delete_object(Bucket="s3bucketname", Key="s3filepath")
Comments
-
Suhail almost 2 years
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?
-
Suhail about 14 yearsyes, i am using that python library, but will that delete, the file ? should i do it this way: k.key = 'images/anon-images/small/'+filename k.delete_key() is this correct ? please let me know.
-
T.J. Crowder about 14 years@Suhail: I haven't used that library, but from the source I linked, what it's actually doing is a
DELETE
call via the REST interface. So yes, despite the name "delete_key" (which I agree is unclear), it's really deleting the object referenced by the key. -
Illarion Kovalchuk about 14 yearsWhat about removing lot of files with a common prefix in name? Does S3 allow some bulk delete for such case, or deleting them one by one (which is slow) is the must?
-
T.J. Crowder about 14 years@Shaman: I'm not an S3 expert, but as far as I know, you can only delete a specific file. But you probably want to actually ask that as a question so it gets attention from S3 experts.
-
Illarion Kovalchuk about 14 yearsRight after commenting here I've added such a question. It has 2 views yet :)
-
jontsai almost 12 yearsIf you wanted to delete EVERYTHING in a bucket, you could do:
for x in b.list(): b.delete_key(x.key)
-
Artur Sapek about 11 yearsI love how in my file it turns out to be
bucket.list()
-
Randall Cook about 11 yearsIt's not exactly pythonic to invoke a subshell to communicate with S3 (a library or a direct HTTP transaction would be more elegant), but it still works. I don't think it deserves a downvote. +1.
-
Nick Chammas about 10 yearsFor this code snippet to work as presented, you'll need to import
Bucket
andKey
, too. As in:from boto.s3.connection import S3Connection, Bucket, Key
-
Harry Moreno about 7 yearsI get
>>> from boto.s3.connection import S3Connection, Bucket, Key Traceback (most recent call last): File "<console>", line 1, in <module> ImportError: No module named boto.s3.connection
please update the answer to boto3 -
Harry Moreno about 7 yearsfigured it out and wrote up a solution harrymoreno.com/2017/04/24/…
-
jarmod about 6 years@Rob The boto3 documentation is misleading. it will create a delete marker if the object is versioned. It will delete the object otherwise.
-
Akash Tantri over 5 yearsIf the object is not present will it throw an error?
-
Kohányi Róbert over 5 years@AkashTantri I haven't personally tried, but the doc says it removes the null version (if there is one) [...] If there isn't a null version, Amazon S3 does not remove any objects. So my guess is that it won't throw an error. If you happen to try it (just do something like
s3.Object('existing-bucket', 'bogus-key').delete()
and see what happens. Also trys3.Object('bogus-bucket', 'bogus-key').delete()
. -
yunus over 5 yearsWorks like a charm , Thats the real power of python
-
PaulB almost 5 yearsClean and simple. Could be the accepted answer, and should definitely be merged with @Kohányi Róbert s answer as both are best approaches for the task.
-
Underoos over 4 yearsDoes the
your-key
here means the actual file name inyour-bucket
on S3? -
Kohányi Róbert over 4 years@SukumarRdjf Yes :)
-
FlyingZebra1 about 4 yearsif the object does not exist - no error is thrown. which is a bummer, would be nice to get the confirmation, or an error message, saying 'object doesn't exist'. meh
-
mojoken about 4 yearsJust thought I'd pass this on: When I tried this passing your-key as s3://my-bucket-name/folder/subfolder/filename the delete() call returned success (HTTPStatusCode 204) but didn't actually delete the file. Then I tried as just folder/subfolder/filename and it worked - same HTTPStatusCode but really deleted the file.
-
Jaspreet Jolly about 4 yearsYes the status code remains 204 whether file is present or not which is not ideally correct as when file doesn't exists it should return error status code or anything.So that user can differentiate and handle it properly.
-
dasilvadaniel about 4 yearsYou can use it to check if the file exists before delete it: obj_exists = list(s3.Bucket('bucket').objects.filter(Prefix='key') if len(obj_exists) > 0 and obj_exists[0].key == 'key': s3.Object('bucket','key').delete()
-
Erik Hambardzumyan over 2 yearsIs there a way to delete a directory using s3fs?