none
EWS item.delete(Delete.Harddelete) taking longer time RRS feed

  • Question

  • We are using EWS(exchaneg 2010 sp1) and reading 1000 emails from journaling mailbox and saving it in db.

    After reading each item we are deleting it. But delete (hard delete) is taking longer time

    Our code looks like below

          FindItemsResults<Item> findResults = _ExchangeService.FindItems(folderId, new ItemView(1000));

          if (findResults.Items.Count > 0)

          {

              foreach (Item item in findResults.Items)

              {

                  if (item is EmailMessage)

                  {

                    //Load the item so that its members will get populated

                    item.Load();

                   SaveToDB(item);

                    item.Delete(Delete.Harddelete)

                  }

                }

          }

    Now to get more speed we have used a new thread for doing item.delte

    Ie.

    foreach (Item item in findResults.Items)

    {

     //  Erlier code

      Thread thread = new Thread(new ParameterizedThreadStart(DeleteEmail));

       thread.Start(((EmailMessage)item));

    }

    In delete email I am calling item.delete.

    I have got two queries

    1. Is there any way other than threading such that item.delete won’t take much time
    2. If threading is the only option should  we move code from load to delete in entire new thread?
    3. Is there any better option to reduce time of item.delete(better threading desing)

    Note: we have requirement that it should read emails , save it and remove emails from journaling mailbox at one go of executable so cann't delete mails in other exe.

    We are using c#

    Regards,

    ABhagwat

    Saturday, March 23, 2013 8:18 PM

Answers

  • If you are going to delete multiple Items then you would be better to batch the Item delete. You should also use LoadPropertiesFromItems http://blogs.msdn.com/b/exchangedev/archive/2010/03/16/loading-properties-for-multiple-items-with-one-call-to-exchange-web-services.aspx instead of doing the Load insitu in the loop which will mean the GetItem in backgroup will be batched as well (doing it insitu will give you really poor performance on a large number of Item. eg some like this will clear the Inbox of Items filtered on a certain type using batching

                ItemView iv = new ItemView(1000);
                FindItemsResults<Item> fiResults = null;
                do
                {
                    fiResults = service.FindItems(WellKnownFolderName.Inbox, iv);
                    PropertySet prPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
                    List<ItemId> idItemIds = new List<ItemId>();
                    service.LoadPropertiesForItems(fiResults.Items, prPropSet);
                    foreach (Item itItem in fiResults.Items) {
                        if(itItem.Subject.Contains("blah")){
                            idItemIds.Add(itItem.Id);
                        }
                    }
                    service.DeleteItems(idItemIds, DeleteMode.SoftDelete, SendCancellationsMode.SendToNone, AffectedTaskOccurrence.AllOccurrences); 
                    iv.Offset += fiResults.Items.Count;
                } while (fiResults.MoreAvailable == true);
    Cheers
    Glen
    • Marked as answer by ABBhagwat Monday, March 25, 2013 9:51 AM
    Monday, March 25, 2013 1:25 AM

All replies

  • If you are going to delete multiple Items then you would be better to batch the Item delete. You should also use LoadPropertiesFromItems http://blogs.msdn.com/b/exchangedev/archive/2010/03/16/loading-properties-for-multiple-items-with-one-call-to-exchange-web-services.aspx instead of doing the Load insitu in the loop which will mean the GetItem in backgroup will be batched as well (doing it insitu will give you really poor performance on a large number of Item. eg some like this will clear the Inbox of Items filtered on a certain type using batching

                ItemView iv = new ItemView(1000);
                FindItemsResults<Item> fiResults = null;
                do
                {
                    fiResults = service.FindItems(WellKnownFolderName.Inbox, iv);
                    PropertySet prPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
                    List<ItemId> idItemIds = new List<ItemId>();
                    service.LoadPropertiesForItems(fiResults.Items, prPropSet);
                    foreach (Item itItem in fiResults.Items) {
                        if(itItem.Subject.Contains("blah")){
                            idItemIds.Add(itItem.Id);
                        }
                    }
                    service.DeleteItems(idItemIds, DeleteMode.SoftDelete, SendCancellationsMode.SendToNone, AffectedTaskOccurrence.AllOccurrences); 
                    iv.Offset += fiResults.Items.Count;
                } while (fiResults.MoreAvailable == true);
    Cheers
    Glen
    • Marked as answer by ABBhagwat Monday, March 25, 2013 9:51 AM
    Monday, March 25, 2013 1:25 AM
  • Hi Glen,

    Thanks for the input I will try the same. I will update the thread once done.

    Regards,

    ABhagwat

    Monday, March 25, 2013 6:48 AM
  • Hi Glen,

    I changed the code as mentioned by you and it worked and performance is increased drastically and  processing time is within my requirement.

    Thanks again ,

    Regards,

    ABhagwat

    Monday, March 25, 2013 9:50 AM