27

After about a dozen deployments, the wwwroot directory is filled with a lot of files due to active development and deployments. We are using VSTS's Azure App Service Deploy task to deploy to Azure Websites Windows Server for a ASP.NET Web API project, is there a way to clean up the wwwroot directory before deploying to the Avsts pre-deployment clean up?

imgen
  • 2,803
  • 7
  • 44
  • 64
  • 4
    What type of website is this (MVC, .Net Core, some other non-.net), and how is it getting deployed (e.g. Web Deploy)? The `Azure App Service Deploy` task has an option to **remove additional files at destination** which might do the trick – Brendan Green Oct 18 '17 at 02:38
  • Thanks for the tips and suggestions, I'll update the question and also try your tip of using **remove additional files at destination** – imgen Oct 18 '17 at 02:39
  • Ok given the updates to the question, this is the option you want (expand out the "Additional Deployment Options" section, and you'll see the option. When checked, this will remove any files **that are not present in the deployment package**. – Brendan Green Oct 18 '17 at 02:47
  • Yes, I tried your tip, it's working. But on caveat is that it only works if we check the `Publish using Web Deploy` option, and it requires `Windows build agent`. But fortunately we are using `Windows build agent`. Thanks again for the excellent tip. – imgen Oct 18 '17 at 03:16
  • @BrendanGreen could you write up an answer so that I can mark it as accepted? – imgen Oct 18 '17 at 03:17
  • Glad to help - answer has been written. – Brendan Green Oct 18 '17 at 03:54

4 Answers4

56

When using the Azure App Service Deploy task, and you are using the Publish using Web Deploy option, there is an additional option to Remove Additional Files at Destination.

enter image description here

If you check this option, the deployment process will remove any files at the destination where there is no corresponding file in the package that is being deployed.

In other words, it'll remove any left over files from a previous deployment that are no longer required.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
  • 3
    What if I don't want to use "Web Deploy" but also want to "Remove Additional Files at Destination"? Is there another way I could do it in the "Azure Web Service Deploy" task? – Dave Jul 09 '18 at 07:54
  • 1
    Use something like this? https://marketplace.visualstudio.com/items?itemName=geertvdcruijsen.GeertvanderCruijsen-Vsts-Release-AzureWebAppVFS – Brendan Green Jul 09 '18 at 11:44
3

For a manually process and for those not having VSTS, you can use Kudu console to delete the files/folders in Azure Web App.

To access the site/files through the Kudu console use the below URL.

https://****.scm.azurewebsites.net/ (enter your website name instead of ****)

Click on the Debug Console -> PowerShell to open a console along with files, then select and delete the files/folders using the option available.

Gerard
  • 2,649
  • 1
  • 28
  • 46
3

For Web Deploy use answer by @Brendan Green.

For Zip Deploy, from Kudu documentation:

When a new build is deployed with zipdeploy, files and directories that were created by the previous deployment but are no longer present in the build will be deleted. Any other files and directories found in the site that aren't being overwritten by the deployment, such as those placed there via FTP or created by your app during runtime, will be preserved.

https://github.com/projectkudu/kudu/wiki/Deploying-from-a-zip-file-or-url

I think it's good enough for most cases.

Alexander S.
  • 844
  • 1
  • 13
  • 29
1

If using App Service Linux you can't use web deploy and the "Remove Additional Files at Destination" option is not possible.

A workaround in this case is to use the 'Post Deployment Action' with an 'inline script' set to: rm -rf /home/site/wwwroot/*

You can have 2x Azure App Service deploy tasks, the first one deploying anything (for example, an empty zip file) and including the above script, the 2nd task to deploy your app code.

This approach is only suitable if you are using a deployment slot and slot swap, (which is recommended anyway), otherwise your app will break during deployment when the files are deleted!

Andy
  • 160
  • 1
  • 9