Outlook macro to permanently delete items in junk email folder

If you are anything like me you hate using the mouse to right click on your Outlook Junk E-mail Folder to select Empty "Junk E-mail" Folder and then on top of that you have to click Yes when it asks you "Are you sure you want to permanently delete all the items and subfolder in the "Junk E-mail" folder?". I hate doing that and I'm more of a hotkey kind of person so I wanted some way to do this with a hotkey.

A while ago I remember KC Lemson posted a macro to delete the items in your Junk E-mail folder, which works great except that it just puts the items from the Junk E-mail folder into the Deleted Items folder where as I wanted to permanently delete them. I modified her macro so that it permanently deletes the items from the Junk E-mail folder (well sort of).

In the Outlook Object Model there is no direct way to permanently delete an item, so I found this post which suggested we just delete the item twice, once from the original folder and once from the Deleted Items folder, which seems to be a pretty simple work around. Anyway here is the macro code:

Public Sub EmptyJunkEmailFolder()

    Dim outApp As Outlook.Application
    Dim junkFolder As Outlook.MAPIFolder
    Dim junkItem, deleteItem As Object
    Dim entryID As String
    
    Set outApp = CreateObject("outlook.application")
    Set junkFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderJunk)
    
    For Each junkItem In junkFolder.Items
        entryID = junkItem.entryID  ' Store item entry id
        junkItem.Delete             ' Delete from junk mail folder
               
        ' To permanently delete item find and delete from deleted items
        Set deleteItem = outApp.Session.GetItemFromID(entryID)
        deleteItem.Delete
    Next
    
    Set junkItem = Nothing
    Set deleteItem = Nothing
    Set junkFolder = Nothing
    Set outApp = Nothing
    
End Sub

Once you setup this macro you can add a toolbar button and set the accelator key to something unique and then you have a hotkey to permanently delete the items from your Junk E-mail folder. If you need help doing any of those things take a look at KC's macro post which has some good information and links on how to do those things.

Also incase anyone is in search for the Outlook or Office Object Model Documentation here is a link that tells you how to find it (it took me longer than I care to say to find this ;).

UPDATE: This msdn article talks you through how to create a personal ditigal signature so Outlook doesn't complain every time you run the macro.

 

14 Comments

  • I always hate having unread items in my deleted items folder. Obviously I am not going to ever read them, or I wouldn't have deleted them in the first place. But some I want to keep in the deleted items, just in case ;) So this macro will delete just those deleted items that I want gone. It's basically the same code as above, but it might save someone a few minutes...


    Public Sub DeleteUnreadItemsFromDeletedItems()

    Dim outApp As Outlook.Application
    Dim deletedItemsFolder As Outlook.MAPIFolder
    Dim junkItem, deleteItem As Object
    Dim entryID As String

    Set outApp = CreateObject("outlook.application")
    Set deletedItemsFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems)

    For Each deletedItem In deletedItemsFolder.Items
    If deletedItem.UnRead = True Then
    deletedItem.Delete
    End If
    Next

    Set deletedItem = Nothing
    Set deletedItemsFolder = Nothing
    Set outApp = Nothing

    End Sub

  • Thanks, works great!

  • Calling "Item.Remove()" seems to permanently delete a message. This is what I came up with:

    Sub EmptyJunk()
    Do While ActiveExplorer.Session.GetDefaultFolder(olFolderJunk).Items.Count > 0
    ActiveExplorer.Session.GetDefaultFolder(olFolderJunk).Items.Remove (1)
    Loop
    End Sub

  • Ashley's post on 10/26/2006 is excellent. Works great - thanks!

  • Anyone got Outlook 2007 that found a fix to keep this macro working?

    Just upgraded to it and the macro doesn't do anything? Thanks, N B

  • The reason it doesn't work is because the EntryID's change when you move the items between the folders. One think I was thinking of that could work is to create a folder under the Deleted Items then move all the items into that folder and then delete that folder. I will post some code do this when I get some time.

  • How would one modify this to work on a folder other than a default folder? In my case, I use SpamBayes to send messages to a folder I've named _Spam and would like to be able to automate the emptying of that folder since I can't just send spam to the Deleted Items folder.

  • The last item is not deleted with this script

  • Just tried the following change in Ashley code...
    Public Sub EmptyJunk()
    Do While ActiveExplorer.Session.GetDefaultFolder(olFolderJunk).Items.Count > 0
    ActiveExplorer.Session.GetDefaultFolder(olFolderJunk).Items.Remove (1)
    Loop
    Do While ActiveExplorer.Session.GetDefaultFolder(olFolderDeletedItems).Items.Count > 0
    ActiveExplorer.Session.GetDefaultFolder(olFolderDeletedItems).Items.Remove (1)
    Loop
    End Sub

  • If I take Avi's code and want to add in sub-directories of my Inbox, how do I do so?

    Thanks!

  • I use a similiar method to automatically mark Junk Email folder messages as read when they come in. I do this because I don't want to have the notification area evelope to display when all that is new for mail is spam. The problem with the maco is that it doesn't refresh the unread mail count that appears next to the junk email folder. How would one refresh this number after the macro has run? And then of course I am taking a leap assuming the notification area envelope will disappear after the refresh finds no more unread messages. Thanks!

  • Why don't they have a way to record macro's in Outlook so that those of us that are intimidated by the prospect of writing the syntax can use them?

  • This worked fine until latest service pack when it needed multiple runs to clear folder. I have modified to (below) which works fine

    Public Sub EmptyJunkEmailFolder()

    Dim outApp As Outlook.Application
    Dim junkFolder As Outlook.MAPIFolder
    Dim junkItem, deleteItem As Object
    Dim EntryID As String
    Dim Count As Integer

    Set outApp = CreateObject("outlook.application")
    Set junkFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderJunk)

    Count = junkFolder.Items.Count
    Do Until Count = 0
    EntryID = junkFolder.Items.Item(Count).EntryID
    junkFolder.Items.Item(Count).Delete
    Set deleteItem = outApp.Session.GetItemFromID(EntryID)
    deleteItem.Delete
    Count = junkFolder.Items.Count
    Loop

    Set deleteItem = Nothing
    Set junkFolder = Nothing
    Set outApp = Nothing

    End Sub

  • Not sure if anybody's watching this anymore, but I have a quick questions. This macro works great for me when I want to delete the junk email out of my personal folder's junk folder. However I'm also using Outlook Connector to access my personal hotmail accout through Outlook. Is there anyway to modify this macro to change which junk folder i want to empty?
    Thanks,
    Kyle
    kyle_beckman@hotmail.com

Comments have been disabled for this content.