ERROR_INSUFFICIENT_ACCESS_TO_SITE_FOLDER

I was publishing an ASP.NET Core application, straight to production, which is a no-no, no-go and got this error, also seen in Figure 1.

image

Figure 1, publishing to an Azure App Service, straight to production

Severity    Code    Description Project File    Line    Suppression State
Error       Web deployment task failed. (Unable to perform the operation (“Delete File”) 
for the specified directory (“D:\home\site\wwwroot\dotnet2dot1.exe”). This can occur
  if the server administrator has not authorized this operation for the user credentials
  you are using.  Learn more at: http://go.microsoft.com/fwlink
/?LinkId=221672#ERROR_INSUFFICIENT_ACCESS_TO_SITE_FOLDER.)

Unable to perform the operation (“Delete File”)  for the specified directory
  (“D:\home\site\wwwroot\dotnet2dot1.exe”). This can occur if the server administrator
has not authorized this operation for the user credentials you are using.  Learn more at:
http://go.microsoft.com/fwlink/
?LinkId=221672#ERROR_INSUFFICIENT_ACCESS_TO_SITE_FOLDER.
The error code was 0x80070005.
Access to the path “D:\home\site\wwwroot\dotnet2dot1.exe”; is denied.
    at Microsoft.Web.Deployment.NativeMethods.RaiseIOExceptionFromErrorCode
         (Win32ErrorCode errorCode, String maybeFullPath)
    at Microsoft.Web.Deployment.FileEx.Delete(String path)
    at Microsoft.Web.Deployment.FilePathProviderBase.Delete(Boolean whatIf) 
     dotnet2dot1     0

When you deploy directly to the production slot of an Azure App Service and there is traffic on the app or the assemblies are loaded into the W3WP process, then you can get errors like this.  If this happens and you must deploy directly to production then stop and/or recycle the Azure App Service which would unload the assemblies and release any handles preventing the deployment from being successful.

The best case is to create a deployment slot, deploy and test on the slot and then swap once you are ready to go live with it.  Here are some articles I wrote about Azure App Service slots:

This is also a very informative article “Dealing with locked files during deployment”.