Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot delete a file from s3 with boto3

I am trying to permanently delete a file from S3, using boto3. My buckets are not using versioning.

I have tried two ways:

def remove_aws_object(bucket_name, item_key):
    ''' Provide bucket name and item key, remove from S3
    '''
    s3_client = boto3.client('s3',
                             aws_access_key_id=AWS_ACCESS_KEY,
                             aws_secret_access_key=AWS_SECRET)
    delete = s3_client.delete_object(Bucket=bucket_name, Key=item_key)
    print(delete)
{'ResponseMetadata': {'RequestId': '61F3C195D373B0C5', 'HostId': 'pigcx1wtIN+Y8RU3zJKliWXcXrHXHzpdAOuGBL64x3V9YefQbWXPZi9B/9F', 'HTTPStatusCode': 204, 'HTTPHeaders': {'x-amz-id-2': 'pigcx1wtIN+Y8RU3zJKliWXcXrHXHzpdAOuGBL64x3V9YefQbWXPZi9B/9FLazUSsCds8f4=', 'x-amz-request-id': '61F3C195D373B0C5', 'date': 'Thu, 13 Feb 2020 09:07:50 GMT', 'server': 'AmazonS3'}, 'RetryAttempts': 0}}

As well as via the Object:

def remove_aws_object(bucket_name, item_key):
    ''' Provide bucket name and item key, remove from S3
    '''
    s3_client = boto3.resource('s3',
                             aws_access_key_id=AWS_ACCESS_KEY,
                             aws_secret_access_key=AWS_SECRET)

    my_object = s3_client.Object(bucket_name, item_key)
    a = my_object.delete()
    print(a)
    {'ResponseMetadata': {'RequestId': '6074B9CA870773CE', 'HostId': 'rQoK+x+xcjAw2T3DpTHMWQb4Gq6DzPJy2qFoFHQCYoGwb8/p7700+Nk+6gBIERN', 'HTTPStatusCode': 204, 'HTTPHeaders': {'x-amz-id-2': 'rQoK+x+xcjAw2TKK833DpTHMWQb4Gq6DzPJy2qFoFHQCYoGwb8/p7700+Nk+6gBIE', 'x-amz-request-id': '6074B9CA870773CE', 'date': 'Thu, 13 Feb 2020 12:02:36 GMT', 'x-amz-version-id': 'null', 'x-amz-delete-marker': 'true', 'server': 'AmazonS3'}, 'RetryAttempts': 0}, 'DeleteMarker': True, 'VersionId': 'null'}

Both methods work without error, but do not remove the file, I can still see it in the S3 console and I then have to manually delete it. Am I doing something wrong?

like image 701
crooksey Avatar asked Oct 16 '25 15:10

crooksey


1 Answers

To reproduce your situation, I did the following:

  • Created a new bucket in the Amazon S3 management console
  • Uploaded a file to the bucket via the management console
  • Used your code (first example) to delete the object

The object was successfully deleted.

So, the code is fine.

like image 131
John Rotenstein Avatar answered Oct 19 '25 09:10

John Rotenstein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!