aws cli s3 bucket remove object with date condition

久未见 提交于 2019-12-05 21:30:05

You're on the right track. To understand what's going on, let's look at what your commands are doing step by step.

aws s3 ls --recursive s3://uat-files-transfer-storage/ | awk '$1 < "2018-02-01 11:13:29" {print $0}' | sort -n

This command lists all files in the bucket recursively, checks the output for a specific condition, and then sorts the resultant output lines.

aws s3 rm --recursive s3://uat-files-transfer-storage/ | awk '$1 < "2018-02-01 11:13:29" {print $0}' | sort -n

This command deletes all the files in your bucket recursively, checks the output for a specific condition, and then sorts the resultant output lines. So the second command deletes all your files first!

What you want to do is list all the files in your bucket, check that they meet a certain criteria, and then delete them. This should work:

aws s3 ls --recursive s3://uat-files-transfer-storage/ | awk '$1 < "2018-02-01 11:13:29" {print $4}' | xargs -n1 -t -I 'KEY' aws s3 rm s3://uat-files-transfer-storage/'KEY'

BE CAREFUL! This won't prompt you before deleting, and it's an easy way to clean out your whole bucket by mistake.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!