How to Delete Messages in Bulk or One by One from Outlook PST in .NET Apps

This technical tip explains how .NET developers can Delete Messages from outlook PST. Add Messages to PST Files showed how to add messages to PST files. It is, of course, also possible to delete items (contents) from a PST file and it may also be desirable to delete messages in bulk. Items from a PST file can be deleted using the FolderInfo.DeleteChildItem() method. The API also provides FolderInfo.DeleteChildItems() method to delete items in bulk from the PST file. This articles shows how to Delete Items from a PST file one by one and delete Items in Bulk from a PST file.
//your code here...//Deleting Messages from PST Files //The code snippets below delete messages from a PST file's Sent subfolder. //[C# Code Sample] // Get the Sent items folder FolderInfo folderInfo = pst.GetPredefinedFolder(StandardIpmFolder.SentItems); MessageInfoCollection msgInfoColl = folderInfo.GetContents(); foreach (MessageInfo msgInfo in msgInfoColl) { Console.WriteLine(msgInfo.Subject + ": " + msgInfo.EntryIdString); if (msgInfo.Subject.Equals("some delete condition") == true) { // Delete this item folderInfo.DeleteChildItem(msgInfo.EntryId); Console.WriteLine("Deleted this message"); } } //[VB.NET Code Sample] ' Get the Sent items folder Dim folderInfo As FolderInfo = pst.GetPredefinedFolder(StandardIpmFolder.SentItems) Dim msgInfoColl As MessageInfoCollection = folderInfo.GetContents() For Each msgInfo As MessageInfo In msgInfoColl Console.WriteLine(msgInfo.Subject & ": " & msgInfo.EntryIdString) If msgInfo.Subject.Equals("some delete condition") = True Then ' Delete this item folderInfo.DeleteChildItem(msgInfo.EntryId) Console.WriteLine("Deleted this message") End If Next msgInfo //Delete Items in Bulk from PST File //Aspose.Email API can be used to delete items in bulk from a PST file. This is achieved using the DeleteChildItems method which accepts a list of Entry ID items referring to the items to be deleted. //[C# Code Sample] using (PersonalStorage pst = PersonalStorage.FromFile(@"test.pst")) { FolderInfo inbox = pst.RootFolder.GetSubFolder("Inbox"); // find messages having From = "" PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder(); queryBuilder.From.Contains(""); MessageInfoCollection messages = inbox.GetContents(queryBuilder.GetQuery()); IList<string> deleteList = new List<string>(); foreach (MessageInfo messageInfo in messages) { deleteList.Add(messageInfo.EntryIdString); } // delete messages having From = "" inbox.DeleteChildItems(deleteList); } //[VB.NET Code Sample] Using pst As PersonalStorage = PersonalStorage.FromFile("test.pst") Dim inbox As FolderInfo = pst.RootFolder.GetSubFolder("Inbox") ' find messages having From = "" Dim queryBuilder As New PersonalStorageQueryBuilder() queryBuilder.From.Contains("") Dim messages As MessageInfoCollection = inbox.GetContents(queryBuilder.GetQuery()) Dim deleteList As IList(Of String) = New List(Of String)() For Each messageInfo As MessageInfo In messages deleteList.Add(messageInfo.EntryIdString) Next ' delete messages having From = "" inbox.DeleteChildItems(deleteList) End Using


Language: C# | User: Sheraz Khan | Created: Jun 8, 2016 | Tags: Delete Messages from PST Files Delete Items in Bulk from PST Delete Items from PST file one by one add messages to PST file Outlook PST to MSG C#, .NET Outlook API