Empty all Deleted Items Folders at the same time

Sabarish Raghupathy runs a personal blog but one of his articles has some Outlook VBA code to empty the Deleted Items folders for multiple mailboxes. Visit the article for the full code.

The code works well but requires some hardcoding of mailbox names. It also requires adding code blocks if you happen to have more than three mailboxes (or removing them if you have less). We can get around that though can't we!

As we know, the mailbox hierarchy in Outlook is as follows:

Mailbox Hierarchy

For a nice Visio chart of the object model, visit Outlook 2003 Object Model Map.

Our NameSpace object holds the names of all the mailboxes in the local copy of Outlook! We can leverage this by using a For loop to iterate through every mailbox regardless of how many there are or what their names are (a limitation of the code in the blog article I mentioned earlier).

So without further delay here is the code I came up with.

Sub EmptyAllTrash()
' loop through all mailboxes and empty deleted items folders
' based on http://sabarish.net/blog/archives/437
Dim olApp As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim objExpl As Outlook.Explorer
Dim mailboxCount As Long
Dim i As Long
Dim deletedItemsFolder As Outlook.MAPIFolder
Dim objCBB As CommandBarButton

' get local namespace
Set olApp = Application
Set olNS = olApp.GetNamespace("MAPI")
Set objExpl = olApp.ActiveExplorer

' loop through mailboxes
mailboxCount = olNS.Folders.Count
For i = 1 To mailboxCount
  On Error Resume Next
  Set deletedItemsFolder = olNS.Folders(i).Folders("Deleted Items")
  If Err = 0 Then
    On Error GoTo 0
    'Select the target folder
    objExpl.SelectFolder deletedItemsFolder
    'Get the Empty Deleted Items command = UID 1671
    Set objCBB = objExpl.CommandBars.FindControl(, 1671)
    'Execute the command
    objCBB.Execute
  End If
Next i

  ' go back to default Inbox
  objExpl.SelectFolder olNS.GetDefaultFolder(olFolderInbox)

End Sub

Hopefully this code is self-explanatory. We loop through the NameSpace.Folders collection and set an object reference to each Deleted Items folder. (Some mailboxes may not have this so we just want to skip them.) Then we select the folder and execute the button code for emptying the folder. The command button isn't available in later versions of Outlook; in that case, you could always loop through the Deleted Items folder using a technique found at Delete Expired Items.

Now add this to a toolbar button and you are all set to keep your Deleted Items folder nice and empty!

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button

Related Articles:



comment bubble 3 Comments on Empty all Deleted Items Folders at the same time:

  1. En Kerro writes:

    I have Outlook 2010, in which I've got two mailboxes; Outlook_PPB and Outlook_PPI. The first one is the default account. In both of these boxes I've got a "Deleted" folder. When I try your code, it empties the default account's "Deleted" folder just fine, but leaves the Outlook_PPI/Deleted folder intact. What am I doing wrong here? :) Is it the Outlook 2010, is something different there?

    By the way, I might not be able to check this page often enough, so, can I please ask you to send your reply via email too. Thank you very much in advance!

    best, En

  2. ~J~ writes:

    I have tried both sets of code. I orignally used the code from sabarish's site and it works at least half the time. I don't know why it fails, but I suspect that it may be that one of my deleted items for a backup folder gets to be 30+ messages in size. That is not a problem for the main deleted items folder. I have four "Deleted Items" folders. I tried your code and it only empties the main folder and skips the others. Any ideas why his or yours would fail or only work sometimes. I have macros enabled. Thanks for any insight. ~J~

This article is closed to new comments. Why?
Excel School