Eugene Astafiev

How To: Delete a folder in Outlook

    Some time ago I showed a way of creating a folder in Outlook. Now I want to show the reverse operation – deleting a folder. The Outlook Object Model provides the Delete method of the Folder class for such task. It doesn’t accept any parameters and doesn’t return any value. It just does its job!
    It is not necessary to delete mail items in the folder first. They will be deleted with the folder automatically. However, Outlook doesn’t allow deleting IPM subtree folders (such as Inbox, Deleted Items, Sent Items, Outbox and etc.). Instead, a user is allowed to delete the contents of such folders, i.e. items in them.
    The following code deletes the Inbox subfolder we created in my previous article. To get the code running you need to pass an instance of the Outlook Application class to the DeleteInboxSubFolder method. In Add-in Express based add-ins you can retrieve it using the OutlookApp property of the add-in module and in VSTO based add-ins please use the Application property of the add-in class. So, let’s get to work!
    C# and Add-in Express:
private void DeleteInboxSubFolder(Outlook._Application OutlookApp)
{
      Outlook.NameSpace nameSpace = OutlookApp.GetNamespace("MAPI");
      Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder(
              Outlook.OlDefaultFolders.olFolderInbox);
      Outlook.Folders inboxSubfolders = folderInbox.Folders;
      for (int i = 1; inboxSubfolders.Count >= i; i++)
      {
          Outlook.MAPIFolder subfolderInbox = inboxSubfolders.Item(i);
          if (subfolderInbox.Name == "InboxSubfolder")
          {
               try
               {
                   subfolderInbox.Delete();
               }
               catch (COMException exception)
               {
                   if (exception.ErrorCode == -2147352567)
                        // Cannot delete this folder. Right-click the folder,
                        // and then click Properties to check your permissions
                        // for the folder. See the folder owner or your
                        // administrator to change your permissions.
                        System.Windows.Forms.MessageBox.Show(exception.Message);
               }
          }
          if (subfolderInbox != null) Marshal.ReleaseComObject(subfolderInbox);
     }
     if (inboxSubfolders != null) Marshal.ReleaseComObject(inboxSubfolders);
     if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
     if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
}
    VB.NET and Add-in Express:
Private Sub DeleteInboxSubFolder(Application As Outlook.Application)
     Dim mapiNameSpace As Outlook.NameSpace = Application.GetNamespace("MAPI")
     Dim folderInbox As Outlook.MAPIFolder = mapiNameSpace.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderInbox)
     Dim inboxSubfolders As Outlook.Folders = folderInbox.Folders
     For i As Integer = 1 To inboxSubfolders.Count
          If inboxSubfolders.Count < i Then Continue For
          Dim subfolderInbox As Outlook.MAPIFolder = inboxSubfolders.Item(i)
          If subfolderInbox.Name = "InboxSubfolder" Then
              Try
                  subfolderInbox.Delete()
              Catch exception As COMException
                  If exception.ErrorCode = -2147352567 Then
                       ' Cannot delete this folder. Right-click the folder,
                       ' and then click Properties to check your permissions
                       ' for the folder. See the folder owner or your
                       ' administrator to change your permissions.
                      System.Windows.Forms.MessageBox.Show(exception.Message)
                  End If
              End Try
          End If
          If Not IsNothing(subfolderInbox) Then Marshal.ReleaseComObject(subfolderInbox)
      Next
      If Not IsNothing(inboxSubfolders) Then Marshal.ReleaseComObject(inboxSubfolders)
      If Not IsNothing(folderInbox) Then Marshal.ReleaseComObject(folderInbox)
      If Not IsNothing(mapiNameSpace) Then Marshal.ReleaseComObject(mapiNameSpace)  
End Sub
    C# and VSTO:
using System.Runtime.InteropServices;
//...
private void DeleteInboxSubFolder(Outlook.Application application)
{
      Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
      Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderInbox);
      Outlook.Folders inboxSubfolders = folderInbox.Folders;
      for (int i = 1; inboxSubfolders.Count >= i; i++)
      {
          Outlook.MAPIFolder subfolderInbox = inboxSubfolders[i];
          if (subfolderInbox.Name == "InboxSubfolder")
          {
               try
               {
                   subfolderInbox.Delete();
               }
               catch (COMException exception)
               {
                   if (exception.ErrorCode == -2147352567)
                        // Cannot delete this folder. Right-click the folder,
                        // and then click Properties to check your permissions
                        // for the folder. See the folder owner or your
                        // administrator to change your permissions.
                       System.Windows.Forms.MessageBox.Show(exception.Message);
               }
         }
         if (subfolderInbox != null) Marshal.ReleaseComObject(subfolderInbox);
     }
     if (inboxSubfolders != null) Marshal.ReleaseComObject(inboxSubfolders);
     if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
     if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
}
    VB.NET and VSTO:
Imports System.Runtime.InteropServices
'...
Private Sub DeleteInboxSubFolder(Application As Outlook.Application)
    Dim mapiNameSpace As Outlook.NameSpace = Application.GetNamespace("MAPI")
    Dim folderInbox As Outlook.MAPIFolder = mapiNameSpace.GetDefaultFolder(
          Outlook.OlDefaultFolders.olFolderInbox)
    Dim inboxSubfolders As Outlook.Folders = folderInbox.Folders
    For i As Integer = 1 To inboxSubfolders.Count
        If inboxSubfolders.Count < i Then Continue For
            Dim subfolderInbox As Outlook.MAPIFolder = inboxSubfolders(i)
            If subfolderInbox.Name = "InboxSubfolder" Then
                Try
                    subfolderInbox.Delete()
                 Catch exception As COMException
                     If exception.ErrorCode = -2147352567 Then
                          ' Cannot delete this folder. Right-click the folder,
                          ' and then click Properties to check your permissions
                          ' for the folder. See the folder owner or your
                          ' administrator to change your permissions.
                          System.Windows.Forms.MessageBox.Show(exception.Message);
                     End If
                  End Try
             End If
        If Not IsNothing(subfolderInbox) Then Marshal.ReleaseComObject(subfolderInbox)
     Next
     If Not IsNothing(inboxSubfolders) Then Marshal.ReleaseComObject(inboxSubfolders)
     If Not IsNothing(folderInbox) Then Marshal.ReleaseComObject(folderInbox)
     If Not IsNothing(mapiNameSpace) Then Marshal.ReleaseComObject(mapiNameSpace)
End Sub
    A message box will inform the user if such folder cannot be deleted. See you on our forums and in the e-mail support!

2 Comments

  • Ariel Mears says:

    What if the folders you wanted to delete were part of the solutions module in outlook? I created them with the ADXOLSolution control.

  • Andrei Smolin (Add-in Express Team) says:

    In my tests (Outlook 2013), the code isn’t able to delete the root folder created by the Outlook Solution Module. It can delete any of the subfolders, it seems. Also, if I unregister the Outlook Solution Module, then the code can also delete the root folder as well. Hope this helps.

Post a comment

Have any questions? Ask us right now!