Deleting Folders in Exchange 2010

Last modified: July 27, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

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

Example

The following example shows how to delete a folder.

static void DeleteFolder(ExchangeServiceBinding esb)
{
    // 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;

    try
    {
        // 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);
        Console.ReadLine();
    }
}

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

<DeleteFolder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                  DeleteType="HardDelete">
  <FolderIds xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <FolderId Id="AQApAH"  xmlns="https://schemas.microsoft.com/exchange/services/2006/types" />
  </FolderIds>
</DeleteFolder>

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

<DeleteFolderResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                      xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ResponseMessages xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <DeleteFolderResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
    </DeleteFolderResponseMessage>
  </ResponseMessages>
</DeleteFolderResponse>

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 MicrosoftVisual 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.

Note

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

Compiling the Code

For information about compiling the code, see EWS Client Development in Exchange 2010.