Search code examples
msdeploywebdeploy

MSDeploy API - Deleting a remote file through code


I am looking to remotely fire a delete command using the MSDeploy API through c# code.

I want to achieve the following command:

msdeploy.exe -verb:delete -dest:contentPath="/folderName/filename.txt"

instead of through running an unmanaged external executable, I want to execute this using the MSDeploy .Net API.


Solution

  • I've found the answer thanks to Richard Szalay's leading and i've used the ContentPath provider as this is a common provider used by VS Publishing so the chances of having permissions is high:

    var deployBaseOptions = new DeploymentBaseOptions
    {
        ComputerName = "https://mywebserver.com:8172/msdeploy.axd?sitename=yourIISWebsiteName",
        UserName = "username",
        Password = "password",
        UseDelegation = true,
        AuthenticationType = "Basic"
    };
    var syncOptions = new DeploymentSyncOptions
    {
        DeleteDestination = true
    };
    var deploymentObject = DeploymentManager.CreateObject(DeploymentWellKnownProvider.ContentPath,
                                                            "yourIISWebsiteName" + "/fileToDelete.txt",
                                                            destBaseOptions);
    
    var results = deploymentObject.SyncTo(deployBaseOptions, syncOptions);
    

    The weird thing is that results always shows 3 files deleted even when there is only one...?!