Share via


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.

Example

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 = @"http://FQDN.com/EWS/Exchange.asmx";

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

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 Exchange Web Services Client Development.