I received a question last week asking how to use MSDeploy to delete a web applicaiton in IIS. This particular use case was for cleaning up old web service versions. That’s no problem, MSDeploy can do that too, using the lesser known and/or under utilized delete verb.

From the console

You can simply call MSDeploy.exe with the iisApp provider delete verb to delete a web application. You may omit the source attribute when using the delete verb.

>"c:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:delete -dest:iisApp="Default Web Site/app13" Info: Deleting application (Default Web Site/app13) Info: Deleting iisApp (Default Web Site/app13). Info: Deleting MSDeploy.iisApp (MSDeploy.iisApp). Total changes: 3 (0 added, 3 deleted, 0 updated, 0 parameters changed, 0 bytes copied)

From WEbDeploy

You can also add the deletion of old versions from within an existing WebDeploy deployment as well, by extending the WebDeploy manifest. First we will define an ItemGroup of old version application names to delete in our wpp.targets file:

<ItemGroup> <VersionsToDelete Include="Default Web Site/ServiceV1" /> <VersionsToDelete Include="Default Web Site/ServiceV2" /> <VersionsToDelete Include="Default Web Site/ServiceV3" /> </ItemGroup>

Next we will create a MSBuild Target which will eventually extend our WebDeploy manifest. We need the target to be called before the manifest file is created so we add our target to the GenerateMsdeployManifestFilesDependsOn property:

<PropertyGroup> <GenerateMsdeployManifestFilesDependsOn> $(GenerateMsdeployManifestFilesDependsOn); RemoveOldServiceVersions </GenerateMsdeployManifestFilesDependsOn> </PropertyGroup> <Target Name="RemoveOldServiceVersions" DependsOnTargets="GetMSDeployInstalledVersionPath"> </Target>

Additionally note the DependsOnTargets attribute on the target, this ensures the MSDeployPath property is set before we try to use it.

Lastly, we extend the WebDeploy manifest by adding a runCommand provider in the MsDeploySourceManifest ItemGroup from within our new target.

<Target Name="RemoveOldServiceVersions" DependsOnTargets="GetMSDeployInstalledVersionPath"> <ItemGroup> <MsDeploySourceManifest Include="runCommand"> <Path>"$(MSDeployPath)msdeploy.exe" -verb:delete -dest:iisApp="%(VersionsToDelete.Identity)"</Path> </MsDeploySourceManifest> </ItemGroup> </Target>

The Path node specifies the same command as the one above for running from the console. The only difference is we are instructing this item to be added for each item in the VersionsToDelete item group by using the item metadata syntax “%(ItemGroup.Property)”.

For more detail on extending WebDeploy see my Extending the WebDeploy Manifest post.

I also added this functionality to the previous ServiceVersioningWithMSDeploy source on Github. Check it out if you want to try it for yourself.

Hope this helps others. If you have any questions please leave a comment below or continue the discussion on Twitter/Reddit.