Last active
May 6, 2024 15:03
-
-
Save simlated/fbbd5ae40b59bb3b0c1449b989a68804 to your computer and use it in GitHub Desktop.
Remove objects from an AWS s3 bucket if last modified date is egual or before a given date.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usage: ./remove-s3-objects-by-date.sh bucket_name min_date | |
# min_date parameter must be in YYYY-MM-DD format | |
if [ "$1" = "" ]; then | |
echo "Missing bucket name. Usage:./remove-s3-objects-by-date.sh bucket_name min_date" | |
exit 1 | |
fi | |
if [ "$2" = "" ]; then | |
echo "Missing min date. Usage:./remove-s3-objects-by-date.sh bucket_name min_date" | |
exit 2 | |
fi | |
bucket_name=$1 | |
min_date=$2 | |
read -p "Are you sure you want to delete objects from $bucket_name having last modified date <= $min_date (y/n)? " -n 1 -r | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
objects=($( aws s3 ls s3://$bucket_name --recursive | awk -v md="$min_date" '$1<=md {print $4}')) | |
for object_name in "${objects[@]}" | |
do | |
: | |
#echo $object_name | |
aws s3 rm s3://$bucket_name/$object_name | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment