Skip to content

Instantly share code, notes, and snippets.

@halilduygulu
Last active April 24, 2023 17:57
Show Gist options
  • Save halilduygulu/b44d4df778980778a9b4bf4da4ec9806 to your computer and use it in GitHub Desktop.
Save halilduygulu/b44d4df778980778a9b4bf4da4ec9806 to your computer and use it in GitHub Desktop.
AWS S3 CLI command to filter and delete objects by file size from bucket, 1000 by 1000 using s3api delete-objects command.
### S3 Bulk Delete by File Size ###
aws s3 ls s3://YOUR_BUCKET_NAME/content/article/data/ --profile YOUR_AWS_PROFILE --recursive | awk -F ' ' '{print $3,$4}' | awk -F ' ' '$1 < 100 {print $2}' | xargs -IP echo '{"Key": "P"}' > delete.txt
#Because bulk delete limit is 1000 per api call.
split -l 1000 delete.txt
#Append json start and end parts to complete a bulk delete request in every file.
for file in x*; do
echo '{"Objects": [' >> delete"$file" && paste -d, -s "$file" >> delete"$file" &&
echo '],"Quiet": true }' >> delete"$file"
done
#Send delete requests as json content in delete* files.
for file in deletex*; do
aws s3api delete-objects --bucket YOUR_BUCKET_NAME --delete file://"$file" --profile YOUR_AWS_PROFILE
done
@zachliu
Copy link

zachliu commented Mar 9, 2022

cleaned it up a bit

#!/bin/bash

# This is to remove super large s3 files

set -e

PROFILE="YOUR_AWS_PROFILE"

S3_BUCKET="YOUR_BUCKET_NAME"
S3_URL="s3://$S3_BUCKET/content/article/data/"
SIZE_THRESHOLD="50000000"  # 50MB

aws s3 ls $S3_URL \
  --profile $PROFILE \
  --recursive | \
  awk -F ' ' '{print $3,$4}' | \
  awk -v size=$SIZE_THRESHOLD -F ' ' '$1 > size {print $2}' | \
  xargs -IP echo '{"Key": "P"}' \
  > delete.txt

# Because bulk delete limit is 1000 per api call.
split -l 1000 delete.txt

# Append json start and end parts to complete a bulk delete request in every file.
for file in x*; do
  echo '{"Objects": [' > delete_"$file" && \
    paste -d, -s "$file" >> delete_"$file" && \
    echo '],"Quiet": true }' >> delete_"$file"
done

# Send delete requests as json content in delete* files.
for file in delete_x*; do
  aws s3api delete-objects --bucket $S3_BUCKET --delete file://"$file" --profile $PROFILE
done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment