This documentation is archived and is not being maintained.

Deleting folders by using the EWS Managed API 2.0

EWS Managed API

Last modified: October 13, 2012

Applies to: EWS Managed API | Exchange Server 2007 Service Pack 1 (SP1) | Exchange Server 2010

Note: This content applies to the EWS Managed API 2.0 and earlier versions. For the latest information about the EWS Managed API, see Web services in Exchange.

You can use the Microsoft Exchange Web Services (EWS) Managed API to delete folders from a mailbox.

To delete a folder from a mailbox

  1. Bind to the folder to delete. In the following example, the FolderId object named folderId identifies the folder to delete. The ExchangeService object named service contains the settings to bind to Exchange Web Services.

    Folder folder = Folder.Bind(service, folderId);

    You cannot delete WellKnownFolderName folders.

  2. Delete the folder. The following example shows how to delete the folder by using the HardDelete option


    The following table lists all the options that you can use to delete a folder.

    Delete folder option



    A folder is permanently removed from the store.


    A folder is moved to the Deleted Items folder.


    A folder is moved to the dumpster if the dumpster is enabled.

The following code example shows how to permanently delete a folder from a mailbox.

// To delete a folder.
Folder folder = Folder.Bind(service, folderId);

The following example shows the XML that is sent by the Delete method. The FolderId Id and ChangeKey attributes have been shortened to preserve readability.

<soap:Envelope xmlns:xsi="" 
    <t:RequestServerVersion Version="Exchange2010" />
    <m:DeleteFolder DeleteType="HardDelete">
        <t:FolderId Id="AAMkADEzOTExYjJkLTYxZDAtNAA=" 
                    ChangeKey="AQAAABYAAACo21KPF" />

The following example shows the XML that is returned by using the Delete method.

<soap:Envelope xmlns:soap="" 
    <t:ServerVersionInfo MajorVersion="8" 
                         xmlns:t="" />
    <m:DeleteFolderResponse xmlns:t="" 
        <m:DeleteFolderResponseMessage ResponseClass="Success">

For information about compiling this code, see Getting started with the EWS Managed API 2.0.

  • Write appropriate error handling code for common search errors.

  • Review the client request XML that is sent to the Exchange server.

  • Review the server response XML that is sent from the Exchange server.

  • Set the service binding as shown in Setting the Exchange service URL by using the EWS Managed API 2.0. Do not hard code URLs because if mailboxes move, they might be serviced by a different Client Access server. If the client cannot connect to the service, retry setting the binding by using the AutodiscoverUrl(String) method.

  • Set the target Exchange Web Services schema version by setting the requestedServerVersion parameter of the ExchangeService constructor. For more information, see Versioning EWS requests by using the EWS Managed API 2.0.

  • Use HTTP with SSL for all communication between client and server.

  • Always validate the server certificate that is used for establishing the SSL connections. For more information, see Validating X509 certificates by using the EWS Managed API 2.0.

  • Do not include user names and passwords in trace files.

  • Verify that Autodiscover lookups that use HTTP GET to find an endpoint always prompt for user confirmation; otherwise, they should be blocked.