I want to create an Azure function that deletes files from azure blob storage when last modifed older than 30 days. Can anyone help or have a documentation to do that?
-
Hi Dani, welcome to SO. You've got some downvotes because people answering questions usually like to see that you have put in some work already. So did you do some research/googling yourself and at what exact point are you blocked. You can definitely do this with Azure Functions. Have a look at [Timer Triggers](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer) and the [CloudBlobClient](https://learn.microsoft.com/en-us/azure/storage/common/storage-samples-dotnet?toc=%2fazure%2fstorage%2fblobs%2ftoc.json) – Marc Jul 05 '19 at 13:49
-
Please see my answer [here](https://stackoverflow.com/a/65702387/838301) for a similar scenario – Abdul Rauf Jan 13 '21 at 12:55
3 Answers
Assuming your storage account's type is either General Purpose v2 (GPv2) or Blob Storage, you actually don't have to do anything by yourself. Azure Storage can do this for you.
You'll use Blob Lifecycle Management and define a policy there to delete blobs if they are older than 30 days and Azure Storage will take care of deletion for you.
You can learn more about it here: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts.
- 128,066
- 12
- 206
- 241
-
I can't for the life of me find an option for "Lifecycle Management". Is it still there? – Murphybro2 Nov 06 '20 at 15:38
-
4@Murphybro2 it is under 'Storage Account' -> 'Blob Service' -> 'Lifecycle Management' – Anton Matsiuk Mar 04 '21 at 15:07
-
1I found it under 'Storage Account' -> 'Data Management' -> 'Lifecycle management'. – Chris Mar 02 '22 at 10:02
You can create a Timer Trigger function, fetch the list of items from the Blob Container and delete the files which does not match your criteria of last modified date.
- Create a Timer Trigger function.
- Fetch the list of blobs using CloudBlobContainer.
- Cast the blob items to proper type and check LastModified property.
- Delete the blob which doesn't match criteria.
I hope that answers the question.
- 591
- 6
- 14
I have used HTTP as the trigger as you didn't specify one and it's easier to test but the logic would be the same for a Timer trigger etc. Also assumed C#:
[FunctionName("HttpTriggeredFunction")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
[Blob("sandbox", Connection = "StorageConnectionString")] CloudBlobContainer container,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Get a list of all blobs in your container
BlobResultSegment result = await container.ListBlobsSegmentedAsync(null);
// Iterate each blob
foreach (IListBlobItem item in result.Results)
{
// cast item to CloudBlockBlob to enable access to .Properties
CloudBlockBlob blob = (CloudBlockBlob)item;
// Calculate when LastModified is compared to today
TimeSpan? diff = DateTime.Today - blob.Properties.LastModified;
if (diff?.Days > 30)
{
// Delete as necessary
await blob.DeleteAsync();
}
}
return new OkObjectResult(null);
}
Edit - How to download JSON file and deserialize to object using Newtonsoft.Json:
public class MyClass
{
public string Name { get; set; }
}
var json = await blob.DownloadTextAsync();
var myClass = JsonConvert.DeserializeObject<MyClass>(json);
- 3,113
- 5
- 24
- 46
-
this is super helpful. any idea how to deserialize each blob into a specific DTO? – Alex Gordon Jul 08 '19 at 03:13
-
@l--''''''---------'''''''''''' not quite sure what you mean? What is the content of your blob? – Chris Jul 08 '19 at 07:54
-
it would be a json payload that i would like to deserialize into a regular c# clas – Alex Gordon Jul 08 '19 at 14:06