Python Forum
Delete files from amazon s3 bucket
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete files from amazon s3 bucket
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  delete all files and subdirectory from a main folder mg24 7 1,762 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  delete all files which contains word sql_Table1 mg24 2 942 Sep-15-2022, 10:05 PM
Last Post: mg24
  Delete empty text files [SOLVED] AlphaInc 5 1,685 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  boto3 Amazon SQS raghuvamsi59 1 1,597 Oct-01-2019, 04:32 AM
Last Post: raghuvamsi59
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 3,030 Sep-22-2019, 05:49 PM
Last Post: ndc85430
  Delete Lines that Contain Words - Loop through files in a folder - Write to new files dj99 3 3,568 May-18-2019, 06:34 AM
Last Post: heiner55
  Delete multiple files using file extention sumncguy 2 2,815 Jan-23-2019, 03:05 AM
Last Post: snippsat
  How to generate more MP3 files at the same time in Amazon Polly using Python code? makiwara 2 3,849 Jul-02-2018, 08:43 PM
Last Post: makiwara
  lambda-Amazon EC2:Remove instance termination protection if enabled and terminate ins dragan979 0 3,409 Jun-13-2018, 09:48 AM
Last Post: dragan979
  Looping through files, check content and delete metalray 1 2,443 May-11-2018, 02:16 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020