Share via


Deleting Items in Exchange 2010

Last modified: July 27, 2009

Applies to: Exchange Server 2007 | Exchange Server 2010

You can use Exchange Web Services to delete items from a mailbox. The DeleteItem request is formed the same way when you are deleting messages, contacts, tasks, and calendar items.

Example

The following example shows how to delete a message item in the Exchange store.

static void DeleteItem(ExchangeServiceBinding esb)
{
    // Create the DeleteItem request.
    DeleteItemType dit = new DeleteItemType();
    dit.ItemIds = new BaseItemIdType[1];

    // Create an item ID type and set the ID.
    ItemIdType itemId = new ItemIdType();

    // Get the ID from the message that you obtained by using FindItem or CreateItem.
    itemId.Id = "AQAtAE";

    dit.ItemIds[0] = itemId;

    // Delete the message.
    DeleteItemResponseType diResponse = esb.DeleteItem(dit);

    // Check the result.
    if (diResponse.ResponseMessages.Items.Length > 0 &&
        diResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
    {
        Console.WriteLine("Message with Id {0} is deleted.", itemId.Id);
    }
}

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

<?xml version="1.0" encoding="utf-8"?>
<DeleteItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                DeleteType="HardDelete">
  <ItemIds xmlns="https://schemas.microsoft.com/exchange/services/2006/messages">
    <ItemId Id="AQAtAE" 
            xmlns="https://schemas.microsoft.com/exchange/services/2006/types" />
  </ItemIds>
</DeleteItem>

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

<?xml version="1.0" encoding="utf-8"?>
<DeleteItemResponse 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">
    <DeleteItemResponseMessage ResponseClass="Success">
      <ResponseCode>NoError</ResponseCode>
    </DeleteItemResponseMessage>
  </ResponseMessages>
</DeleteItemResponse>  

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 item identifier has been shortened to preserve readability. Item identifiers are returned in the FindItem, CreateAttachment, CreateItem (when the item is saved and not sent), GetAttachment, GetEvents, GetItem, and UpdateItem responses.

Compiling the Code

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