Delete Pyrax Cloud Block Storage Volumes Snapshots Older than N days

This very simple example shows you how to delete snapshots taken of rackspace cloud volumes older than N days. In this example N days will be 7 days.

This example makes the following assumptions:
-you have a Pyrax installed
-you have your Rackspace API credentials key.
-you know your Rackspace volume uid – which looks like this: 56568786-4659-67575-9ab3-6767968

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pyrax
from datetime import timedelta, datetime 
 
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credentials('daveid', '56576767ee04e6a80691cf565769b345b')
 
cbs = pyrax.cloud_blockstorage
 
snapshots = cbs.list_snapshots()
 
for snapshot in snapshots:
    create_date = snapshot.created_at
    create_date = create_date.split(".")[0]
    create_date = datetime.strptime(create_date, '%Y-%m-%dT%H:%M:%S')
    if datetime.now() - create_date > timedelta(days = 7):
        print "older" 
        snapshot.delete()

Comments are closed.