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
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.
@less(item()?['LastModified'], addDays(utcNow(), -7))
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.
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.