Created
March 20, 2017 20:50
-
-
Save ashee/ea8a07e9461eb962fde19d572315402f to your computer and use it in GitHub Desktop.
Bulk delete s3 objects
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
#!/usr/bin/env ruby | |
require 'aws-sdk' | |
BUCKET_NAME = 'YOUR_BUCKET_NAME' | |
PREFIX = 'your/key/path/' | |
s3 = Aws::S3::Resource.new(region: 'us-east-1') | |
bucket = s3.bucket(BUCKET_NAME) | |
# find files with an embedded colon (:) | |
keys_to_delete = bucket.objects(prefix: PREFIX) | |
.select {|o| o.key =~ /.*:.*/} | |
.collect() { |o| | |
{ :key => "#{o.key}"} | |
} | |
# delete files found above | |
bucket.delete_objects({ | |
delete: { # required | |
objects: keys_to_delete, | |
quiet: false, | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment