Code sample for deleting all contacts in outlook using C#

 
The following code sample show how to remove or delete all contacts in outlook using c# programming language.
Before using this code sample you need to add reference of ‘Microsoft.Office.Interop.Outlook’ library.

[C#t]

private void DeleteAllContacts()
        {
            Outlook.Application oApp = default(Outlook.Application);
            oApp = Interaction.CreateObject("Outlook.Application");
            Outlook.NameSpace oNs = default(Outlook.NameSpace);
            oNs = oApp.GetNamespace("MAPI");
 
            Outlook.MAPIFolder oFolder = default(Outlook.MAPIFolder);
            Outlook.ContactItem ContactItem = default(Outlook.ContactItem);
            oFolder = oNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
 
            for (long i = 0; i <= oFolder.Items.Count - 1; i++)
            {
                try
                {
                    ContactItem = oFolder.Items.Item(i);
                    ContactItem.Delete();
                }
                catch (System.Exception ex)
                {
                }
            }
            MessageBox.Show("All contacts have been deleted successfully");
 
            oFolder = null;
            oNs = null;
            oApp = null;
        }

2 thoughts on “Code sample for deleting all contacts in outlook using C#”

  1. Hi,
    thanks’s for that code, but how can i delete all contact Items from “DeletedItemsFolder”, which have a special property like ContactItem.user3=”test”?
     
    Thanks
    regards
    Rene

  2. hello Rene
    try this code for deleting contact items from ‘Deleted items’ folder:

    oFolder = oNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems);
    for (long i = 1; i= oFolder.Items.Count; i++) {
    	try {
    		ContactItem = oFolder.Items.Item(1);
    		if (ContactItem.User3 == "test") {
    			ContactItem.Delete();
    		}
    	} catch (System.Exception ex) {
    	}
    }

    thanks
    let me know about the result

Comments are closed.