Skip to content

Instantly share code, notes, and snippets.

@keithio
Created October 21, 2011 00:58
Show Gist options
  • Save keithio/1302834 to your computer and use it in GitHub Desktop.
Save keithio/1302834 to your computer and use it in GitHub Desktop.
Recursively delete everything on an S3 bucket
'''
Copyright (c) 2011, Keith Hall <http://keith.io>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
About:
This script will delete an AWS S3 Bucket. It checks for all files (including
versioned files), and deletes them recursively, if need be. Then, it deletes
the bucket.
This is going to take a while if you have a lot of files. I can't change
that. :(
Usage:
Modify the three (3) variables below with your own information. Then just
run the script.
Warning:
I am not responsible for loss of data! This script is designed to do EXACTLY
that!
'''
import boto
#
# Modify these!
#
AWS_ACCESS_KEY_ID = ''
AWS_SECRET_ACCESS_KEY = ''
BUCKET_TO_DELETE = ''
#
# Don't modify below UNLESS you know what you're doing
#
# Connect to the bucket
s3 = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
bucket = s3.get_bucket(BUCKET_TO_DELETE)
# Delete current items
if bucket.list():
for i in bucket.list():
bucket.delete_key(i)
print '- %s' % i.name
# Delete versioned items
if bucket.list_versions():
for v in bucket.list_versions():
bucket.delete_key(v.name, version_id=v.version_id)
# Delete the bucket
bucket.delete()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment