Fixing the error “Web Deploy cannot modify the file on the Destination because it is locked by an external process.”

"Publishing Failed" for an Azure Function

This post describes how to fix the error, where when publishing Azure Functions or Azure App Services you get an error like this: “Web Deploy cannot modify the file on the Destination because it is locked by an external process.”

This is luckily another straightforward fix! 

Problem

Azure Function Publish fails with a message:

It is, indeed, caused by some of your files at the target of your publishing being in use, so they cannot be overwritten. Great – an actually accurate error message! Much appreciated.

This seems to apply to Azure Functions CLI versions 2.x (currently in beta), and not for the stable versions. At least that’s the state at the time of writing this. There’s even this unresolved issue open about it on GitHub.

Solution(s)

The obvious choice is always to just restart the app service. But if the issue exists every single time when you’re trying to deploy your changes to the server, especially in your development environment, and even more so, if it fails your automated deployments, this solution is cumbersome and boring.

Luckily, you can also just modify the application settings to instruct MSDeploy to rename the files it can’t overwrite – it usually solves this issue! There are 2 ways to do that, whis is kind of nice:

Solution 1: Edit the application settings at the target of your publishing

This one’s nice and simple – just navigate to your Azure Function’s/App Service’s application settings, and add the following key-value pair:

MSDEPLOY_RENAME_LOCKED_FILES = 1

It should look like this:

MSDeploy overwrite/rename application settings

MSDeploy overwrite/rename application settings

After this, the next web deploy you try, should work just fine!

Solution 2: Edit the publishing settings for MSDeploy

You can also edit this setting for MSDeploy directly. In case you’re using Visual Studio for development, you can access the settings by clicking “Publish” on your project, and then clicking “Manage application settings” in the new window.

MSDeploy overwrite/rename setting in Visual Studio publishing settings

MSDeploy overwrite/rename setting in Visual Studio publishing settings

And you should be good!

References

Check out this article by the Kudu team – they explain the issue quite well: https://github.com/projectkudu/kudu/wiki/Dealing-with-locked-files-during-deployment

The following two tabs change content below.

Antti K. Koskela

Solutions Architect / Escalations Engineer at Koskila / Norppandalotti Software / Valo Solutions
Antti Koskela is a proud digital native nomadic millenial full stack developer (is that enough funny buzzwords? That's definitely enough funny buzzwords!), who works as a Solutions Architect for Valo Intranet, the product that will make you fall in love with your intranet. Working with the global partner network, he's responsible for the success of Valo deployments happening all around the world. He's been a developer from 2004 (starting with PHP and Java), and he's been bending and twisting SharePoint into different shapes since MOSS. Nowadays he's not only working on SharePoint, but also on .NET projects, Azure, Office 365 and a lot of other stuff.This is his personal professional (e.g. professional, but definitely personal) blog.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.