thiago lacerda
thiago lacerda

Reputation: 39

AWS delete files older than 30 days aws cli

I need to create a job that validates the oldest files in a bucket, after deleting them. I'm new to aws cli. Can someone help me?

OBS: I'm creating in bash.

Upvotes: 0

Views: 1161

Answers (1)

ericfossas
ericfossas

Reputation: 2196

I would suggest using an S3 Lifecycle expiration rule, but if you have to do this in bash, try this:

BUCKET=""
aws s3api list-objects --bucket "$BUCKET" --query \
     "Contents[?LastModified<=\`$(date -v-30d '+%Y-%m-%d')\`][].{object: Key}" \
  | jq -r '.[].object' \
  | xargs -I {} aws s3 rm s3://$BUCKET/{} --dryrun 

Upvotes: 2

Related Questions