AWS Storage Gateway User Guide
Deleting Snapshots
}
// Returns the date in two formats as a list
public static boolean CompareDates(int daysBack, Date snapshotDate) {
Date today = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(today);
cal.add(Calendar.DAY_OF_MONTH, -daysBack);
Date cutoffDate = cal.getTime();
return (snapshotDate.compareTo(cutoffDate) > 0) ? true : false;
}
}
Deleting Snapshots by Using the AWS SDK for .NET
To delete many snapshots associated with a volume, you can use a programmatic approach. The
following example demonstrates how to delete snapshots using the AWS SDK for .NET version 2 and
3. To use the example code, you should be familiar with running a .NET console application. For more
information, see
in the
AWS SDK for .NET Developer Guide
. If you need to just delete a
few snapshots, use the console as described in
.
Example : Deleting Snapshots by Using the AWS SDK for .NET
In the following C# code example, an AWS Identity and Access Management (IAM) user can list the
snapshots for each volume of a gateway. The user can then determine whether the snapshot start time
is before or after a specified date (retention period) and delete snapshots that have passed the retention
period. The example uses the AWS SDK for .NET API for AWS Storage Gateway and Amazon EC2. The
Amazon EC2 API includes operations for working with snapshots.
The following code example uses the AWS SDK for .NET version 2 and 3. You can migrate older versions
of .NET to the newer version. For more information, see
Migrating Your Code to the Latest Version of the
Update the code to provide the service endpoint, your gateway Amazon Resource Name (ARN), and
the number of days back you want to save snapshots. Snapshots taken before this cutoff are deleted.
You also need to specify the Boolean value
viewOnly
, which indicates whether you want to view the
snapshots to be deleted or to actually perform the snapshot deletions. Run the code first with just
the view option (that is, with
viewOnly
set to
true
) to see what the code deletes. For a list of AWS
service endpoints you can use with AWS Storage Gateway, see
in the
AWS General
Reference
.
First, you create an IAM user and attach the minimum IAM policy to the IAM user. Then you schedule
automated snapshots for your gateway.
The following code creates the minimum policy that allows an IAM user to delete snapshots. In this
example, the policy is named
sgw-delete-snapshot
.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "StmtEC2Snapshots",
"Effect": "Allow",
"Action": [
"ec2:DeleteSnapshot",
"ec2:DescribeSnapshots"
],
"Resource": [
"*"
API Version 2013-06-30
166