{"id":235,"date":"2015-11-09T17:46:40","date_gmt":"2015-11-09T17:46:40","guid":{"rendered":"http:\/\/www.haxed.me.uk\/?p=235"},"modified":"2015-11-09T21:30:53","modified_gmt":"2015-11-09T21:30:53","slug":"deleting-all-the-files-in-a-cloud-container","status":"publish","type":"post","link":"https:\/\/haxed.me.uk\/index.php\/2015\/11\/09\/deleting-all-the-files-in-a-cloud-container\/","title":{"rendered":"Deleting All the Files in a Cloud Container"},"content":{"rendered":"<p>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&#8217;d be really really really fat so maybe that is a bad idea). A dollar though, now there&#8217;s a thought.<\/p>\n<p>On that note, here is a dollar. Probably the best dollar you&#8217;ll see today. You could probably do this with php, bash or swiftly,<strong> but doing it *THIS* way is also awesome<\/strong>, and I <strong> learnt (although some might say learned) something<\/strong>. Here is how I did it. I should also importantly thank Matt Dorn for his contributions to this article. Without him this wouldn&#8217;t exist.<\/p>\n<p>Step 1. Install Python, pip <\/p>\n<pre>\r\nyum install python pip\r\napt-get install python pip\r\n<\/pre>\n<p>Step 2. Install Pyrax (rackspace Python Openstack Library)<\/p>\n<pre>\r\npip install pyrax\r\n<\/pre>\n<p>Step 3. Install Libevent <\/p>\n<pre>\r\ncurl -L -O https:\/\/github.com\/downloads\/libevent\/libevent\/libevent-2.0.21-stable.tar.gz\r\ntar xzf libevent-2.0.21-stable.tar.gz\r\ncd libevent-2.0.21-stable\r\n.\/configure --prefix=\"$VIRTUAL_ENV\"\r\nmake && make install\r\ncd $VIRTUAL_ENV\/..\r\n<\/pre>\n<p>Step 4. Install Greenlet and Gevent<\/p>\n<pre>\r\n\r\npip install greenlet\r\npip install gevent\r\n<\/pre>\n<p>Step 5. Check gevent library loading in Python Shell <\/p>\n<pre>\r\npython\r\nimport gevent\r\n<\/pre>\n<p>If nothing comes back, the gevent lib works OK.<\/p>\n<p>Step 6. Create the code to delete all the files<\/p>\n<pre>\r\n#!\/usr\/bin\/python\r\n# -*- coding: utf-8 -*-\r\nfrom gevent import monkey\r\nfrom gevent.pool import Pool\r\nfrom gevent import Timeout\r\nmonkey.patch_all()\r\nimport pyrax\r\n\r\nif __name__ == '__main__':\r\n    pool = Pool(100)\r\npyrax.set_setting('identity_type', 'rackspace')\r\npyrax.set_setting('verify_ssl', False)\r\n# Rackspace Credentials Go here, Region LON, username: mycloudusername apikey: myrackspaceapikey. \r\npyrax.set_setting('region', 'LON')\r\npyrax.set_credentials('mycloudusername', 'myrackspaceapikey')\r\n\r\ncf = pyrax.cloudfiles\r\n# Remember to set the container correctly (which container to delete all files within?)\r\ncontainer = cf.get_container('testing')\r\nobjects = container.get_objects(full_listing=True)\r\n\r\n\r\ndef delete_object(obj):\r\n\r\n# added timeout of 5 seconds just in case\r\n\r\n    with Timeout(5, False):\r\n        try:\r\n            obj.delete()\r\n        except:\r\n            pass\r\n\r\n\r\nfor obj in objects:\r\n    pool.spawn(delete_object, obj)\r\npool.join()\r\n<\/pre>\n<p>It&#8217;s well worth noting that this can also be used to list all of the objects as well, but that is something for later&#8230;<\/p>\n<p>Step 7. Execute (not me the script!)<\/p>\n<p>The timeout can be adjusted. And the script can be run several times to ensure any missed files are retried to be deleted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;d be really really really fat so maybe that is a bad &hellip; <a href=\"https:\/\/haxed.me.uk\/index.php\/2015\/11\/09\/deleting-all-the-files-in-a-cloud-container\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,15,14,10,9,7,11,12,17,1],"tags":[],"class_list":["post-235","post","type-post","status-publish","format-standard","hentry","category-api","category-cloud","category-cloud-files","category-filesystem","category-linux","category-management-tools","category-openstack","category-openstack-api","category-python","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":2,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions"}],"predecessor-version":[{"id":237,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/posts\/235\/revisions\/237"}],"wp:attachment":[{"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/media?parent=235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/categories?post=235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/haxed.me.uk\/index.php\/wp-json\/wp\/v2\/tags?post=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}