Blog Post

Azure PaaS Blog
2 MIN READ

Delete all the Azure Storage Blob content before N days using Logic App

manish_gupta's avatar
manish_gupta
Icon for Microsoft rankMicrosoft
Sep 05, 2019

Prerequisites:

Blob Storage account which has the blobs inside which needs to be deleted:

 

 

Action:

1- Create the Logic App

2- As soon as you create the Logic app, you get to go to Designer part of it. Since we are creating the recurrence logic, we can start with inbuilt recurrence trigger:

 

3- Once you select the recurrence trigger, you get the screen where you can set the number of recurrences based on the need. As in below screen shot my logic will run once in every week:

 

4- After this you select the “New Step” where you get chance to select your blob storage. Search for Azure Blob Storage and select it:

 

5-After selecting the Blob storage, you have to select the action on that storage and we will select the list blobs operation since we will be listing the blobs first and then will perform further actions:

 

6-After this we need to select the Storage account where we need to list and delete the blobs and give connection name:

 

Click create button and then we need to select the container where our blobs reside and then click on new step:

 

7- Next step, we will create the Array where those blobs will be residing after fetching it. So, search for “Filter Array” keyword and select it:

 

8- After Array selection, we need to select the dynamic value which will be iterated from the array:

Then click on the “Edit in advanced mode” and provide this query “@less(item()?['LastModified'], addDays(utcNow(), -7))” which means that we will be checking the “LastModified” field and will filter which are later than 7 days from today( you can change this value as per your need).

 

9:Click on New Step and search for the “for each” for looping the array:

 

After selecting the for each loop, we need to give dynamic value to it and select the “body” parameter to it:

 

10 - Then click on the “Add an action” where we will be selecting the “Delete Blob” operation:

 

12- After this, we need to select the Blob Path which needs to be deleted and since it will be dynamic, we will choose the “Path” value for this:

 

13-  Now your Logic app designer should look like this and if yes then you can save the changes:

 

15 - Run it:

 

That's all guys. Happy Azure!

Updated Sep 15, 2020
Version 2.0
  • mcdasa's avatar
    mcdasa
    Brass Contributor

    if you have a target files to delete more than hundred or thousnad,

    make sure to check on execution history as executed it was planned.

    logic app will cost you by execution number of action not by cycle. and there is default retry numbers so your action could be executed multiple times.

  • Michael Rybicki's avatar
    Michael Rybicki
    Copper Contributor

    Thanks manish_gupta - I will try to cycle through the containers. Unfortunately Lifecycle Management doesn't work for me because these are all Page Blobs (SQL Server backups) and Lifecycle management doesn't auto-archive those. :facepalm:  Maybe you can put in a good word for me so they will start allowing that.

  • faldana's avatar
    faldana
    Copper Contributor

    Hi, Manish.

    Yep. I got this using the Life Cycle Management, and it is working perfectly for about 20 days.

    Thanks so much for answering my question.

    Regards.

  • faldana I think you will get the nextmarker value in response if you have more than 5000 blobs. you can pass this value again with list blob until you stop getting that parameter. That's one way to deal with that. That;s how even SDK works in the background. 

     

    Second and easy way is to use the Life cycle management policy which is inbuilt in Azure portal and you can achieve the same there

    https://docs.microsoft.com/en-us/azure/storage/blobs/storage-lifecycle-management-concepts?tabs=azure-portal#add-or-remove-a-policy

  • Michael Rybicki's avatar
    Michael Rybicki
    Copper Contributor

    Please disregard previous message. While creating this the designer told me it had create a For Each and I thought it meant for each Blob Container so I accepted it but it did something else (See For each 2 inside loop?). I removed that and I think it's working now.

     

    So how would I do this for each container in the same storage account? I have about 50 different containers that I want to keep clean. And there are certain ones I want to skip. I can't find a way to List all Blob containers in a storage account.

  • Michael Rybicki's avatar
    Michael Rybicki
    Copper Contributor

    HELP!  I added this so I could remove files older than 30 days and it delete ALL FILES in the container!

     

    (Starts with the ampersand but I can't post the message with it in there)

    less(item()?['LastModified'], addDays(utcNow(), -30)

     

    What did I do wrong?

     

  • faldana's avatar
    faldana
    Copper Contributor

    Hi, Manish.

     

    How can we manage the Logic App to run when there are more than 5000 blobs?

    I saw there is a limit of 5000 per execution.

     

    Thanks.