Search code examples
amazon-s3boto3

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?


Solution

  • It was a fairly simple solution, my items are stored within a folder structure, and I needed to pass the folder location, along with the item key.

    def remove_aws_object(bucket_name, item_key):
        ''' Provide bucket name and item key, remove from S3
        '''
        folder = "foo/bar/"
        delete_key = folder + item_key
        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=delete_key)