Deleting Folders (Exchange Web Services)
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Deleting Folders (Exchange Web Services)

Topic Last Modified: 2009-07-16

You can use Exchange Web Services to delete folders by using a unique identifier.

The following example shows how to delete a folder.

static void DeleteFolder()
    // Create the binding and set the credentials.
    ExchangeServiceBinding esb = new ExchangeServiceBinding();
    esb.Credentials = new NetworkCredential("UserName", "Password", "Domain");
    esb.Url = @"";

    // Create the request.
    DeleteFolderType deleteFolderRequest = new DeleteFolderType();
    deleteFolderRequest.DeleteType = DisposalType.HardDelete;

    FolderIdType[] folderIDArray = new FolderIdType[1];
    folderIDArray[0] = new FolderIdType();
    folderIDArray[0].Id = "AQApAH";

    deleteFolderRequest.FolderIds = folderIDArray;

        // Send the request and get the response.
        DeleteFolderResponseType deleteResponse = esb.DeleteFolder(deleteFolderRequest);

        //// Get the response messages
        ResponseMessageType[] rmta = deleteResponse.ResponseMessages.Items;

        foreach (ResponseMessageType rmt in rmta)
            // Cast to the correct response message type.
            if (rmt.ResponseClass == ResponseClassType.Success)
                Console.WriteLine("Folder deleted from mailbox.");
    catch (Exception e)
        Console.WriteLine("Exception: " + e.Message);

The following example shows the XML request message that is sent from the client to the server.

<DeleteFolder xmlns:xsi="" 
  <FolderIds xmlns="">
    <FolderId Id="AQApAH" xmlns="" />

The following example shows the XML response message that is sent from the server to the client.

<DeleteFolderResponse xmlns:xsi="" 
  <ResponseMessages xmlns="">
    <DeleteFolderResponseMessage ResponseClass="Success">

The SOAP messages that are passed between the Exchange Web Services client and server are defined by the XML schema and WSDL files. The XML schema and WSDL files define the contract between the client and server. Proxy class generators create an object-model abstraction of those SOAP messages, which can simplify programming. This code example uses a proxy class library that was generated by Microsoft Visual Studio 2005. Different proxy class generators create different object models for a given Web service. This proxy class code example is an illustration only. Refer to the proxy class generator documentation for support for proxy classes.

The folder ID has been shortened to preserve readability. Folder identifiers are returned in the FindFolder, CopyFolder, CreateFolder, CreateManagedFolder, GetFolder, MoveFolder, and UpdateFolder responses.

For information about compiling the code, see Exchange Web Services Client Development.

© 2016 Microsoft