Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure delete old blobs based on last modified

I would like to add old blobs to a list and then loop through it and delete them.

So if 7 days have passed since the blob was last modified i want to delete it. Blobs got a property named last modified, but it seems like its of type bool (?)

Anyone been down this road before?

Something like this:

CloudBlobContainer container = CloudStorageServices.GetCloudBlobsContainer();

var blobs = container.ListBlobs().OfType<CloudBlockBlob>().Where(b=>b.Properties.LastModified - b.Properties.LastModified.AddDays(7)).TotalHours <= 0);

Thanks!

like image 240
Reft Avatar asked Feb 13 '23 20:02

Reft


1 Answers

You're more or less on the right path. Try the code below. It will fetch the blobs from a container which have not been modified in last 7 days.

    static void GetOldBlobs()
    {
        CloudStorageAccount acc = new CloudStorageAccount(new StorageCredentials("account name", "account key"), false);
        var client = acc.CreateCloudBlobClient();
        var container = client.GetContainerReference("container name");
        var blobs = container.ListBlobs("", true).OfType<CloudBlockBlob>().Where(b => (DateTime.UtcNow.AddDays(-7) > b.Properties.LastModified.Value.DateTime)).ToList();
    }
like image 97
Gaurav Mantri Avatar answered Feb 15 '23 08:02

Gaurav Mantri