Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to batch delete all empty folders in Outlook?

Suppose there are dozens of empty folders under a mail folder in Outlook, generally we can delete the empty folders one by one by right-clicking menu. Compared to right clicking repeatedly, this article will introduce a VBA to quickly delete all empty subfolders of one Outlook folder in bulk.

Batch delete all empty folders in Outlook with VBA

Office Tab - Enable Tabbed Editing and Browsing in Office, and Make Work Much Easier...
Kutools for Outlook - Brings 100 Powerful Advanced Features to Microsoft Outlook
  • Auto CC/BCC by rules when sending email; Auto Forward Multiple Emails by rules; Auto Reply without exchange server, and more automatic features...
  • BCC Warning - show message when you try to reply all if your mail address is in the BCC list; Remind When Missing Attachments, and more remind features...
  • Reply (All) With All Attachments in the mail conversation; Reply Many Emails at once; Auto Add Greeting when reply; Auto Add Date&Time into subject...
  • Attachment Tools: Auto Detach, Compress All, Rename All, Auto Save All... Quick Report, Count Selected Mails, Remove Duplicate Mails and Contacts...
  • More than 100 advanced features will solve most of your problems in Outlook 2010-2019 and 365. Full features 60-day free trial.

arrow blue right bubbleBatch delete all empty folders in Outlook with VBA

To remove all empty subfolders of a certain Outlook folder, please do as follows:

1. Press Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste below VBA code into the new module window.

VBA: Delete all empty subfolders of certain Outlook folder in bulk

Public Sub DeletindEmtpyFolder()
Dim xFolders As Folders
Dim xCount As Long
Dim xFlag As Boolean
Set xFolders = Application.GetNamespace("MAPI").PickFolder.Folders
Do
FolderPurge xFolders, xFlag, xCount
Loop Until (Not xFlag)
If xCount > 0 Then
MsgBox "Deleted " & xCount & "(s) empty folders", vbExclamation + vbOKOnly, "Kutools for Outlook"
Else
MsgBox "No empty folders found", vbExclamation + vbOKOnly, "Kutools for Outlook"
End If
End Sub

Public Sub FolderPurge(xFolders, xFlag, xCount)
Dim I As Long
Dim xFldr As Folder 'Declare sub folder objects
xFlag = False
If xFolders.Count > 0 Then
For I = xFolders.Count To 1 Step -1
Set xFldr = xFolders.Item(I)
If xFldr.Items.Count < 1 Then 'If the folder is empty check for subfolders
If xFldr.Folders.Count < 1 Then 'If the folder contains not sub folders confirm deletion
xFldr.Delete 'Delete the folder
xFlag = True
xCount = xCount + 1
Else 'Folder contains sub folders so confirm deletion
FolderPurge xFldr.Folders, xFlag, xCount
End If
Else 'Folder contains items or (subfolders that may be empty).
FolderPurge xFldr.Folders, xFlag, xCount
End If
Next
End If
End Sub

3. Press F5 Key or Run button to run this VBA code.

4. In the popping out Select Folder dialog box, please select the specific folder whose empty subfolders you will delete in bulk, and click the OK button. See screenshot:

5. Now a Kutools for Outlook dialog box comes out and shows you how many empty subfolders have been deleted. Click the OK button to close it.

Until now, all subfolders of the specified Outlook folder have been deleted in bulk already.


arrow blue right bubbleRelated Articles

Find folder (full folder path) by folder name in Outlook


Kutools for Outlook - Brings 100 Advanced Features to Outlook, and Make Work Much Easier!

  • Auto CC/BCC by rules when sending email; Auto Forward Multiple Emails by custom; Auto Reply without exchange server, and more automatic features...
  • BCC Warning - show message when you try to reply all if your mail address is in the BCC list; Remind When Missing Attachments, and more remind features...
  • Reply (All) With All Attachments in the mail conversation; Reply Many Emails in seconds; Auto Add Greeting when reply; Add Date into subject...
  • Attachment Tools: Manage All Attachments in All Mails, Auto Detach, Compress All, Rename All, Save All... Quick Report, Count Selected Mails...
  • Powerful Junk Emails by custom; Remove Duplicate Mails and Contacts... Enable you to do smarter, faster and better in Outlook.
shot kutools outlook kutools tab 1180x121
shot kutools outlook kutools plus tab 1180x121
 
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Martin · 5 months ago
    74 empty folders were deleted but unfortunately also 109 folders that were not. Other empty folders were left untouched.
  • To post as a guest, your comment is unpublished.
    Adam · 1 years ago
    Super easy and incredibly helpful. Thank you!!
  • To post as a guest, your comment is unpublished.
    Spooz · 1 years ago
    I am getting the same error like Bryan.... and now?
    • To post as a guest, your comment is unpublished.
      Carina · 8 months ago
      The script tries to delete a folder that was already deleted.
      I added a row after xFlag = False with this content:
      on error resume next
  • To post as a guest, your comment is unpublished.
    Bryan · 1 years ago
    I am getting the following error when run the above " Run-time error '-2147352567 (80020009)' 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"

    It appears the script moves 1 item to the deleted folder and then errors out.
    • To post as a guest, your comment is unpublished.
      TechGiant · 1 years ago
      Agree - I get the same error.
      • To post as a guest, your comment is unpublished.
        Carina · 8 months ago
        The script tries to delete a folder that was already deleted.
        I added a row after xFlag = False with this content:
        on error resume next
        • To post as a guest, your comment is unpublished.
          Munier Saliba · 8 months ago
          Indeed, add:

          On Error Resume Next

          AFTER:

          Dim x Fldr As Folder 'Declare sub folder objects
          xFlag = False

          It should look like this:

          Dim x Fldr As Folder 'Declare sub folder objects
          xFlag = False
          On Error Resume Next
  • To post as a guest, your comment is unpublished.
    Miroslav · 1 years ago
    Brilliant!!!