Reading Time: 4 minutes
Follow by Email

Recently I have had to the need to clean up some of my blob storage and keep it clean. This can be done using an Azure PowerShell Runbook, but I wanted to see if it could be done with little to no code. Azure Logic Apps are perfect for this. You can create some powerful stuff with Logic Apps. So in this article I am going to show you how to remove files from Azure Blob Storage after X number of days using Logic Apps.

The Logic App

In the Azure Portal click the green + button and search for Logic Apps.

Click Logic App and then click Create.

Give your new Logic App a Name, select the Subscription you want to create your Logic App in. Then either Create or use an existing Resource Group, pick the Location, turn Log Analytics On or Off, and finally click Create.

After some time the Logic App will be created. Navigate to the Logic Apps blade and then open you new Logic App. You should be greeted with the Logic App Designer.

Scroll down and click on Blank Logic App.

In the Search connectors and triggers box search for Recurrence. Click on the Recurrence trigger.

Set the Interval to how every many days you want the Logic app to run. I am going to have mine run every day. Then click on Show advanced options. Here select your time zone and the Hour of the day you want the logic app to run. I have chosen UTC and midnight. Once finished click Next step.

It’s now time to Choose an action. Search for List Blobs and click the Trigger.

Enter a Connection Name. You will also see a list of your Storage accounts click on the one you want to clean up. Then click Create.

You will now be asked to Specify the folder. Click on the folder icon and select the Container you want to clean up.

Then click New step.

Now search for Filter array and click the trigger.

Select the value button under list blobs.

Click Edit in advanced mode and type the following to clean up files older than 7 days. Then click Next step.

Now we need a For Each.

Click Body and then click Add an action.

Search for Delete a blob and click it.

Click in the box and then click Path.

You logic App is now ready to be Saved. To do that just click the Save icon.

That’s it, your newly created Logic App will now run at midnight every day and it will remove any files that are older than 7 days.

Prove it!

Let’s just test the Logic App to make sure it’s going to work as excepted. As you can see in my Blob storage, I have a few files and they have different modified dates. Some are older than 7 days and some are newer.

If you navigate to the Logic App you just created. We can Run it on demand.

Now if you go back to your Blob Storage you should see that any files older than 7 days are gone.

This method of cleaning up files in Blob storage is very easy and does not need any special Azure Run As accounts like you would if you were using Azure automation runbooks.

I hope you found this article helpful, if you have any comments reach out in the usual methods.

Follow by Email

Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.


Brian · December 13, 2018 at 2:20 am

Brilliant! A guide to exactly what I am trying to do. I thought I’d have to piece things together between several articles, Stack Overflow questions, and documentation. Thanks for the super clear instructions and screenshots!

Naga · May 3, 2019 at 7:26 pm

I tried this but the files are not deleting with my criteria. I can see the files with my Powershell script and storage explorer. Any pointers how to validate whether i am getting output.

    Pixel Robots. · May 5, 2019 at 8:39 am

    Are you able to show your logic app settings and the details of the files?

      Naga · May 6, 2019 at 5:50 pm

      The filter itself , it is not getting the proper data in LogicaApps. using ” @less(item()?[‘LastModified’], addDays(utcNow(), -31))” , it was able to fetch only 2 records. Actually there were 29. I have written Powershell script which will cleanup but want to automate in Azure.

Danilo · July 3, 2019 at 2:56 pm

Hi everything worked fine except for the files which are leased. To test the logic, I needed to break the lease before running it.
Is there any way to do it before deleting the file?


Fabio Aldana · March 12, 2021 at 6:38 am

Hi Richard Hooper.
How can we manage when we have more than 5000 blobs to be deleted?
I saw there is a limitation up to 5000 per execution.

Lilian Aduku · September 8, 2021 at 1:16 pm

Thank you this was really useful

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *