Mario Cardinal

"The real voyage of discovery consists, not in seeking new landscapes, but in having new eyes" – Marcel Proust


1 Comment

How to clean up the wwwroot folder on the Azure Web App using PowerShell

I’m writing this post to make sure that a PowerShell recipe to remove all the files in the wwwroot directory on the Azure Web App is documented somewhere on the web. Thus, with the help of Google search, those who will have the same goal in the future will find the right answer easily.

Recently, when deploying the To-Do Studio website with Azure DevOps, I needed to clean the wwwroot directory before deploying. It is usually a simple configuration setting when running the release on a hosted VS2017 agent. When using the Azure App Service Deploy task with the Publish using Web Deploy option, there is an additional option to Remove Additional Files at Destination. Unfortunately, in order to roll the automated E2E tests with Cypress, we must run the release script on a Linux agent. This Remove Additional Files at Destination option is not available anymore on Linux. So I had to automate the cleanup  with an Azure Pipeline task.

At first, to fill this gap, I thought about using the “Azure WebApp Virtual File System Tasks” available in the Azure DevOps marketplace. Unfortunately, this task did not work. So I had to automate the cleanup with a Powershell script.

The non-automated (manual) way to delete files / folders in Azure Web App is to use the Kudu console. For those who like me want to automate this operation with PowerShell, there is KUDU Virtual File System (VFS) Rest API.

Without further ado, here is the script.  You can download it from my Personal GitHub repos or you can copy and paste without restraint from the script below.

$WebAppName = “insert Web App name”
$slotName = “insert Slot Name”
$username = ‘insert the username from the publish profile’ #From the publish profile
$password = “insert the password from the publish profile” #From the publish profile
# Initialize parameters for Invoke-RestMethod
if ($slotName -ne “”){
    $apiUrl = https://$webAppName`-$slotName.scm.azurewebsites.net/api/vfs/site/wwwroot/”
}
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes((“$($username):$($password)”)))
$headers = @{
    Authorization=“Basic $($base64AuthInfo)”
    ‘If-Match’ = ‘*’
}
$userAgent = “powershell/2.0”
# Define a reursive function to delete files
function DeleteKuduDir ($content, $dir)
{
    foreach($c in $content)
    {      
        if($c.mime -eq “inode/directory”)
        {
            # Get listing of directory as an array
            $childContent = Invoke-RestMethod Uri $c.href Headers $headers UserAgent $userAgent Method GET ContentType “application/json”
           
            # Delete directory
            $newDir = $dir + (Split-Path $c.href leaf) + “\”
            DeleteKuduDir content $childContent dir $newDir
        }
        # Delete file
        $file = Split-Path $c.href leaf
        Write-Host “Deleting” $dir$file    
        $result = Invoke-RestMethod Uri $c.href Headers $headers UserAgent $userAgent Method DELETE ContentType “application/json”
    }
}
# Get listing of wwwroot as an array
$rootContent = Invoke-RestMethod Uri $apiUrl Headers $headers UserAgent $userAgent Method GET ContentType “application/json”
# Delete files and directory in wwwroot
DeleteKuduDir content $rootContent dir “\”
Write-Host “Done!”