Skip to content

Instantly share code, notes, and snippets.

@anlai
Created December 28, 2012 16:31
Show Gist options
  • Save anlai/4399376 to your computer and use it in GitHub Desktop.
Save anlai/4399376 to your computer and use it in GitHub Desktop.
Function that deletes files out of an azure storage container, after X amount of days.
private readonly string _storageAccountName;
private readonly string _storageKey;
private readonly string _storageContainer;
private readonly int _cleanupThreshold;
private const string CloudStorageconnectionString = @"DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}";
public IEnumerable<string> BlobCleanup()
{
var storageAccount = CloudStorageAccount.Parse(string.Format(CloudStorageconnectionString, _storageAccountName, _storageKey));
var client = storageAccount.CreateCloudBlobClient();
var container = client.GetContainerReference(_storageContainer);
var blobs = container.ListBlobs(null, true);
var filtered = blobs.Where(a => a is CloudBlockBlob &&
((CloudBlockBlob)a).Properties.LastModified < DateTime.Now.AddDays(_cleanupThreshold)
).ToList();
var deleted = new List<string>();
foreach(var item in filtered)
{
var blob = (CloudBlockBlob)item;
deleted.Add(blob.Name);
blob.Delete();
}
return deleted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment