Deleting old web app logs using Azure Web Jobs and PowerShell
Tags: azure, powershell, webjobs
Published: 2020-09-09 17:38:10

---
title: Deleting old web app logs using Azure Web Jobs and PowerShell
published: true
tags: azure, powershell, webjobs
canonical_url: https://swimburger.net/blog/azure/deleting-old-web-app-logs-using-azure-web-jobs-and-powershell
cover_image: https://swimburger.net/media/1153/deleting-old-web-app-logs-using-azure-social.png?anchor=center&mode=crop&width=1000&height=420&rnd=132066449350000000
---

When your Azure App Service writes lots of logs, these logs can quickly pile up and even reach your "File system storage" quota limits.  
This was something I personally didn't pay attention to for quite some time and was surprised to find multiple gigabytes of logs sitting in my app service.  
To solve this issue, you can use a PowerShell script and a time triggered Azure Web Job.

## Creating a PowerShell script to delete old log files

Azure Web Jobs supports many languages: Windows cmd & exe, PowerShell, Bash, PHP, Python, NodeJS, and Java.  
PowerShell is a really good scripting language to automate many things on both Linux and Windows. You can use the following one-liner PowerShell script to delete files that are older than 30 days.

```powershell
$LogFolder = "D:\home\site\wwwroot\App_Data\Logs";
$DaysToKeepLogsAround = 30;
Get-ChildItem -Path $LogFolder -Recurse -File | Where LastWriteTime  -lt  (Get-Date).AddDays(-$DaysToKeepLogsAround) | Remove-Item -Force
```

## Creating an Azure Web Job to delete old log files

Now that you're script is ready, you can create an Azure Web Job under you Azure App Service using the following steps:

**1. Navigate to the Web Jobs section under your Azure App Service and click "Add".**

![Add Azure Web Job](https://www.swimburger.net/media/1149/cleanlogs-azure-web-job.png)

**2. Fill out the Add WebJob as shown below in the screenshot:**

![Create Clean Logs Azure Web Job](https://www.swimburger.net/media/1150/create-cleanlogs-azure-web-job.png)

WebJob properties:

- **Name** : Give the WebJob a name that doesn't conflict with other web jobs in your app service.
- **File Upload** : Upload the PowerShell script file
- **Type** : Use triggered so you can setup a CRON schedule to run the script on an interval basis.
- **CRON Expression** : Enter a valid CRON expression which specifies on what time interval to execute the script.  
The CRON expression for Web Jobs is using the open-source project NCrontab. Refer to [NCrontab on GitHub](https://github.com/atifaziz/NCrontab) to learn how to build the CRON expression.   
_**You can also test out NCrontab expressions online at ncrontab.swimburger.net.**_    
The WebJob in the screenshot uses " **0 0 0 \* \* \***" which is the equivalent of running the script **every day at midnight**.

**3. Hit OK and you'll be all set.**

To test out the WebJob, you can hit "Run" from the Azure Portal interface.  
**Warning** : Since you're deleting files using a script, it's a good idea to test this out in a safe environment before adding it to your production environment.

That's how you setup an Azure WebJob to delete your old log files using PowerShell, cheers!