"""Image Asset Migration Delete Objects script. Command line script to remove all objects from a bucket and subfolder. Example Usage: $ python delete_destination_objects_script.py """ # !/usr/bin/env python # -*- coding: utf-8 -*- import datetime import logging import os from os.path import dirname from os.path import join import boto3 from dotenv import load_dotenv dotenv_path = join(dirname(__file__), '.env') load_dotenv(dotenv_path) DESTINATION_BUCKET_NAME = os.environ.get('DESTINATION_BUCKET_NAME') DESTINATION_SUB_FOLDER = os.environ.get('DESTINATION_SUB_FOLDER') if __name__ == '__main__': client = boto3.client('s3') s3 = boto3.resource('s3') bucket_to_empty = s3.Bucket(DESTINATION_BUCKET_NAME) logging.basicConfig( filename='delete_objects.log', level=logging.WARNING) count = 0 for obj in bucket_to_empty.objects.filter(Prefix=DESTINATION_SUB_FOLDER): client.delete_object( Bucket=DESTINATION_BUCKET_NAME, Key=obj.key) count += 1 logging.warning('Date of execution: {}'.format(datetime.datetime.now())) logging.warning('{} objects deleted'.format(count))