· 6 years ago · Oct 13, 2019, 05:30 PM
1#!/bin/bash
2
3BUCKET_NAME="<bucket>"
4BUCKET_PATH="<path>/"
5PROFILE="<profile>"
6
7### S3 Bulk Delete by File Size ###
8aws s3 ls "s3://${BUCKET_NAME}/${BUCKET_PATH}" --profile "${PROFILE}" --recursive | awk -F ' ' '{print $3,$4}' | awk -F ' ' '$1 < 1 {print $2}' | xargs -IP echo '{"Key": "P"}' > delete.txt
9
10# Because bulk delete limit is 1000 per api call.
11split -l 1000 delete.txt
12
13# Append json start and end parts to complete a bulk delete request in every file.
14for file in x*; do
15 echo '{"Objects": [' >> delete"$file" && paste -d, -s "$file" >> delete"$file" &&
16 echo '],"Quiet": true }' >> delete"$file"
17done
18
19# Send delete requests as json content in delete* files.
20for file in deletex*; do
21 aws s3api delete-objects --bucket "${BUCKET_NAME}" --delete file://"$file" --profile "${PROFILE}"
22done