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.

 

Published Friday, November 12, 2004 2:53 PM by puzzlehacker

Comments

# re: Outlook macro to permanently delete items in junk email folder

Hi! ;-) This was very helpfull. We cannot complain about MS's products. They make life very interesting! :-D

Tuesday, July 25, 2006 8:21 AM by Johan

# re: Outlook macro to permanently delete unread items in deleted items

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

Wednesday, October 04, 2006 9:28 AM by bthews

# re: Outlook macro to permanently delete items in junk email folder

Thanks, works great!

Friday, October 13, 2006 1:31 PM by BG

# re: Outlook macro to permanently delete items in junk email folder

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

Monday, October 16, 2006 10:13 AM by Ashley

# re: Outlook macro to permanently delete items in junk email folder

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

Monday, February 12, 2007 2:54 PM by RB

# re: Outlook macro to permanently delete items in junk email folder

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

Wednesday, May 09, 2007 10:54 AM by N B

# re: Outlook macro to permanently delete items in junk email folder

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.

Thursday, May 10, 2007 2:02 AM by puzzlehacker

# re: Outlook macro to permanently delete items in junk email folder

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.

Friday, November 16, 2007 4:33 PM by Shawn

# re: Outlook macro to permanently delete items in junk email folder

The last item is not deleted with this script

Wednesday, November 21, 2007 11:37 AM by Me

# re: Outlook macro to permanently delete items in junk email folder

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

Monday, December 31, 2007 5:22 AM by Avi

# re: Outlook macro to permanently delete items in junk email folder

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

Thanks!

Monday, January 07, 2008 5:44 PM by Nick

# re: Outlook macro to permanently delete items in junk email folder

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!

Tuesday, February 05, 2008 12:00 PM by Will

# re: Outlook macro to permanently delete items in junk email folder

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?

Thursday, March 27, 2008 4:35 PM by Rick Roth

# re: Outlook macro to permanently delete items in junk email folder

Hi there, i've been struggling to get my code working. I'm currently saving xls attachments from my emails, but when the xls is an attachment on another email attachment, i can't seem to get it working

Wednesday, March 25, 2009 4:41 AM by Jubbfly

# re: Outlook macro to permanently delete items in junk email folder

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

Wednesday, June 03, 2009 11:44 AM by Je Sewell

# re: Outlook macro to permanently delete items in junk email folder

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

Monday, September 21, 2009 9:47 PM by Kyle Beckman

Leave a Comment

(required) 
(required) 
(optional)
(required)