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")
Share:
174,043
Suhail
Author by

Suhail

http://pixelsspace.com/about/

Updated on July 08, 2022

Comments

  • Suhail
    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
    Suhail about 14 years
    yes, 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
    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
    Illarion Kovalchuk about 14 years
    What 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
    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
    Illarion Kovalchuk about 14 years
    Right after commenting here I've added such a question. It has 2 views yet :)
  • jontsai
    jontsai almost 12 years
    If you wanted to delete EVERYTHING in a bucket, you could do: for x in b.list(): b.delete_key(x.key)
  • Artur Sapek
    Artur Sapek about 11 years
    I love how in my file it turns out to be bucket.list()
  • Randall Cook
    Randall Cook about 11 years
    It'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
    Nick Chammas about 10 years
    For this code snippet to work as presented, you'll need to import Bucket and Key, too. As in: from boto.s3.connection import S3Connection, Bucket, Key
  • Harry Moreno
    Harry Moreno about 7 years
    I 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
    Harry Moreno about 7 years
    figured it out and wrote up a solution harrymoreno.com/2017/04/24/…
  • jarmod
    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
    Akash Tantri over 5 years
    If the object is not present will it throw an error?
  • Kohányi Róbert
    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 try s3.Object('bogus-bucket', 'bogus-key').delete().
  • yunus
    yunus over 5 years
    Works like a charm , Thats the real power of python
  • PaulB
    PaulB almost 5 years
    Clean 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
    Underoos over 4 years
    Does the your-key here means the actual file name in your-bucket on S3?
  • Kohányi Róbert
    Kohányi Róbert over 4 years
    @SukumarRdjf Yes :)
  • FlyingZebra1
    FlyingZebra1 about 4 years
    if 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
    mojoken about 4 years
    Just 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
    Jaspreet Jolly about 4 years
    Yes 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
    dasilvadaniel about 4 years
    You 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
    Erik Hambardzumyan over 2 years
    Is there a way to delete a directory using s3fs?