Python Forum

Full Version: Delete files from amazon s3 bucket
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to delete all files from amazon s3 bucket before executing other tasks that will ingest data.
Basically delete files before uploading new files. This is my code to delete files. It`s running successfully, but is not deleting any files.

import os
import boto3

def clear_bucket(**kwargs):
    aws = AwsHook(aws_conn_id=kwargs['aws_conn']).get_credentials()
    os.environ['aws_access_key_id'] = aws[0]
    os.environ['aws_secret_access_key'] = aws[1]

    bucket=get_vars['aws_bucket']
    prefix=get_vars['aws_folder']
    
    s3 = boto3.client('s3')
    s3.delete_object(Bucket=bucket, Key=prefix )
I also tried with the code below in which it ran successfully but without deleting any files

import boto3

s3 = boto3.resource('s3')
s3.Object('my-bucket', 'my folder').delete()
Any idea why is not deleting files from s3?