#!/bin/bash BUCKET="mybucket" PREFIX="elasticsearch-backup/all-indexes" [ "$(which pv)" != "" ] && echo "Required: 'apt install -y pv'" && exit 1 aws s3api list-objects --output text --bucket $BUCKET --prefix $PREFIX --query 'Contents[].[Key]' | pv -l > /tmp/${BUCKET}.keys split -d -l 1000 /tmp/${BUCKET}.keys ${BUCKET}- # If for any reason the process stops (you loose network connection, for instance), you can continue the job from here for i in $( ls ${BUCKET}-* ) do echo "Deleting $i" cat $i | pv -l | grep -v -e "'" | tr '\n' '\0' | xargs -0 -P1 -n1000 bash -c "aws s3api delete-objects --bucket $BUCKET --delete \"Objects=[$(printf \"{Key=%q},\" \"$@\")],Quiet=true\"" _ rm $i done