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 “”){
}
$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!”
Like this:
Like Loading...
Related
I am the co-founder of To-Do Studio, a software publisher offering online collective workspaces extended with automated guides. As an experimenter and an entrepreneur, I like to seize opportunities that emerge from the unexpected. Since 2004 (15 years in a row), I am a recipient of the Microsoft Most Valuable Professional (MVP) award. MVP status is awarded to credible technology experts who have shown a deep commitment to innovation, passion about technology and a strong community spirit. An experienced DevOps and Scrum practitioner, I have spent nearly 30 years designing large-scale information systems. I am the author of the book "Executable Specifications with Scrum" and the host of the Visual Studio Talk Show, a podcast about software architecture.
Pingback: Migrating Azure Cloud Services to Azure App Service Web Apps – Adam Paternostro