OrderByDescending

Lately I’ve been working on some code that will delete a hierarchical list of files from Azure blob storage.

According to msdn deleting a container blob will cause contained blobs to fail while the delete is in progress.

With this in mind, I didn’t want to rely just on the order that the names were returned to me from CloudBlobClient.ListBlobs(). I needed to get the list of files in order of longest path first to be sure that content is deleted before the container.

As usual, if your goto is Linq for this kind of algorithm, the code is pretty minimal. The example code below has more in the setup than the code needed to do the job.


var list = new List<Uri>();

list.Add(new Uri("http://www.madeup.com/1/"));
list.Add(new Uri("http://www.madeup.com/1/2/"));
list.Add(new Uri("http://www.madeup.com/1/2/3/4/5/6/7/8"));
list.Add(new Uri("http://www.madeup.com/1/2/3/4/"));
list.Add(new Uri("http://www.madeup.com/1/2/3"));
list.Add(new Uri("http://www.madeup.com/1/3/5/7/"));
list.Add(new Uri("http://www.madeup.com/1/4/6/8/10"));
list.Add(new Uri("http://www.madeup.com/1/3/5/7/9"));

foreach(var item in list.OrderByDescending(x => x.AbsolutePath.Length))
{
	Console.WriteLine(item);
}


Note, blobs in Azure storage terms are represented as discrete urls, there is no concept of deleting a folder and that delete cascading to all files it contains. Each delete needs to be made against a single url.