Deleting Files from S3 Bucket in Python
In this tutorial, we will show you how to delete files from S3 bucket using the AWS Boto3 Python library.
First, you must install the latest version of Boto3 Python library using the following command:
pip install boto3
Here is the code to delete a file from S3 bucket in Python:
import boto3 from botocore.exceptions import ClientError ACCESS_SECRET = "your aws access secret" ACCESS_KEY = "your aws access key" s3_client = boto3.client('s3', region_name='us-east-1', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=ACCESS_SECRET) bucket_name = "bucket-one" # filename file_name = "example.txt" # location of the file to delete in S3 bucket key = "folder/"+file_name # deleting file try: s3_client.delete_object(Bucket=bucket_name, Key=key) except ClientError as ex: print("Error: ", ex)