Hey. So if only I had a cake for every customer that asked if we could delete all of their cloud files in a single container for them (i’d be really really really fat so maybe that is a bad idea). A dollar though, now there’s a thought.
On that note, here is a dollar. Probably the best dollar you’ll see today. You could probably do this with php, bash or swiftly, but doing it *THIS* way is also awesome, and I learnt (although some might say learned) something. Here is how I did it. I should also importantly thank Matt Dorn for his contributions to this article. Without him this wouldn’t exist.
Step 1. Install Python, pip
yum install python pip apt-get install python pip
Step 2. Install Pyrax (rackspace Python Openstack Library)
pip install pyrax
Step 3. Install Libevent
curl -L -O https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz tar xzf libevent-2.0.21-stable.tar.gz cd libevent-2.0.21-stable ./configure --prefix="$VIRTUAL_ENV" make && make install cd $VIRTUAL_ENV/..
Step 4. Install Greenlet and Gevent
pip install greenlet pip install gevent
Step 5. Check gevent library loading in Python Shell
python import gevent
If nothing comes back, the gevent lib works OK.
Step 6. Create the code to delete all the files
#!/usr/bin/python # -*- coding: utf-8 -*- from gevent import monkey from gevent.pool import Pool from gevent import Timeout monkey.patch_all() import pyrax if __name__ == '__main__': pool = Pool(100) pyrax.set_setting('identity_type', 'rackspace') pyrax.set_setting('verify_ssl', False) # Rackspace Credentials Go here, Region LON, username: mycloudusername apikey: myrackspaceapikey. pyrax.set_setting('region', 'LON') pyrax.set_credentials('mycloudusername', 'myrackspaceapikey') cf = pyrax.cloudfiles # Remember to set the container correctly (which container to delete all files within?) container = cf.get_container('testing') objects = container.get_objects(full_listing=True) def delete_object(obj): # added timeout of 5 seconds just in case with Timeout(5, False): try: obj.delete() except: pass for obj in objects: pool.spawn(delete_object, obj) pool.join()
It’s well worth noting that this can also be used to list all of the objects as well, but that is something for later…
Step 7. Execute (not me the script!)
The timeout can be adjusted. And the script can be run several times to ensure any missed files are retried to be deleted.