• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Clean up the Junk Email folder

Slipstick Systems

› Outlook › Clean up the Junk Email folder

Last reviewed on January 14, 2020     2 Comments

Applies to: Outlook (classic), Outlook 2010

I see quite a few requests for help like this:

I'm receiving constant spam emails from a specific top-level domain. No legitimate sender I am in contact with would use this TLD and I would like to delete every single message from email addresses in this domain. The problem: they are snagged by the Junk folder, so any rules won't work. I can't effectively browse the Junk Email folder to see if any legitimate emails are in there, because I'm swamped with the spam.

Correct, rules won't work on mail if the junk email filter grabs it first, but you can run the rules on the Junk Email folder when you use Outlook desktop software.

  1. Add the Run Rules Now command to the Quick Access Toolbar or ribbon
    Add Run Rules Now to QAT
  2. Select the Junk Email folder
  3. Click Run Rules Now button you added to the QAT or ribbon
  4. Select the Rule
  5. Click Run Now
    Select rules to run on selected folder

You can also use an ItemAdd macro to watch the Junk Email folder and delete mail as it is added to the Junk Mail folder.

 

Use an ItemAdd macro

This simple macro watches the default Junk Email folder for messages from one top-level domain.

Notes: As written, these macros only work on the default junk email folder. If you have more than one data file, you'd need to watch each Junk Email folder. Or use the macro at the end of the page to run the ItemAdd macro on any folder in your profile. Also, the macro moves the junk mail to the Deleted items folder. VBA doesn't have a permanently delete command, so w need to move it to the Deleted Items and would need to delete it again.

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
 
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")

'Set the folder and items to watch:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderJunk)
Set objItems = objWatchFolder.Items

Set objWatchFolder = Nothing
End Sub

' ### ItemAdd macro
Private Sub objItems_ItemAdd(ByVal Item As Object)
Debug.Print Item.SenderEmailAddress

' change the TLD and the count 
If Right(Item.SenderEmailAddress, 3) = ".br" Then
  Item.Delete
End If

Set Item = Nothing
End Sub
' ### End ItemAdd macro

To watch for multiple top-level domains in the default Junk Email folder, we'll use select case and an array. This allows us to check for TLDs of different lengths.

For the array, you'll use a comma-separated list of domains. If you want to delete longer TLDs (such as .trade), add another case block, where the Case value is the length of the TLD + 1 (for the .)

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
 
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")

'Set the folder and items to watch:
Set objWatchFolder = objNS.GetDefaultFolder(olFolderJunk)
Set objItems = objWatchFolder.Items

Set objWatchFolder = Nothing
End Sub

 ' ## multi-TLD ItemAdd macro
Private Sub objItems_ItemAdd(ByVal Item As Object)
Dim arrTLD 'As variant
Dim lenTLD As Long
Dim i As Long

'Debug.Print Item.SenderEmailAddress
'Debug.Print InStrRev(Item.SenderEmailAddress, ".")

lenTLD = Len(Item.SenderEmailAddress) - InStrRev(Item.SenderEmailAddress, ".") + 1
'Debug.Print lenTLD

Select Case lenTLD
Case 3
  arrTLD = Array(".de", ".br", ".gq", ".tk", ".ga", ".it", ".tr", ".ml")
GoTo checkdomain

Case 4
  arrTLD = Array(".org", ".xyz")
GoTo checkdomain

Case 5
  arrTLD = Array(".live", ".club")
GoTo checkdomain

Case Else ' avoids errors a longer TLD is found
    Exit Sub

End Select

checkdomain:
Debug.Print lenTLD, Right(LCase(Item.SenderEmailAddress), lenTLD)
' Go through the array and look for a match, then do something
For i = LBound(arrTLD) To UBound(arrTLD)
    If Right(LCase(Item.SenderEmailAddress), lenTLD) = arrTLD(i) Then
    Item.Delete
    Exit Sub
    End If
Next i

Set Item = Nothing
End Sub

' ## End  multi-TLD ItemAdd macro

 

Watch folders in another data file

The macros above watch the Junk Email folder in your default data file. To watch the folder in another data file, you need to use the GetFolderPath function. To use it with Shared Exchange mailboxes, you need to use the shared mailbox code instead.

Option Explicit
Private objNS As Outlook.NameSpace
Private WithEvents objItems As Outlook.Items

Private Sub Application_Startup()
 
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")

'Set the folder and items to watch:
' Set objWatchFolder = objNS.GetDefaultFolder(olFolderJunk)
Set objWatchFolder = GetFolderPath("Outlook Data File\Junk Email")

Set objItems = objWatchFolder.Items

Set objWatchFolder = Nothing
End Sub

'### this can be replaced with the multi-TLD ItemAdd macro

Private Sub objItems_ItemAdd(ByVal Item As Object)
Debug.Print Item.SenderEmailAddress

' change the TLD and the count 
If Right(Item.SenderEmailAddress, 3) = ".br" Then
  Item.Delete
End If

Set Item = Nothing
End Sub

' ### End ItemAdd

' from https://slipstick.me/qf
Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
    Dim oFolder As Outlook.Folder
    Dim FoldersArray As Variant
    Dim i As Integer
        
    On Error GoTo GetFolderPath_Error
    If Left(FolderPath, 2) = "\\" Then
        FolderPath = Right(FolderPath, Len(FolderPath) - 2)
    End If
    'Convert folderpath to array
    FoldersArray = Split(FolderPath, "\")
    Set oFolder = Application.Session.Folders.Item(FoldersArray(0))
    If Not oFolder Is Nothing Then
        For i = 1 To UBound(FoldersArray, 1)
            Dim SubFolders As Outlook.Folders
            Set SubFolders = oFolder.Folders
            Set oFolder = SubFolders.Item(FoldersArray(i))
            If oFolder Is Nothing Then
                Set GetFolderPath = Nothing
            End If
        Next
    End If
    'Return the oFolder
    Set GetFolderPath = oFolder
    Exit Function
        
GetFolderPath_Error:
    Set GetFolderPath = Nothing
    Exit Function
End Function

How to use the macros on this page

First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.

To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.

After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.

Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.

The macros on this page need to go into ThisOutlookSession.

Open the VBA Editor by pressing Alt+F11 on your keyboard.

To put the macro code in ThisOutlookSession:

  1. Expand Project1 and double click on ThisOutlookSession.
  2. Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)

More information as well as screenshots are at How to use the VBA Editor

 

How to test an ItemAdd macro

To test an ItemAdd macro, you can either copy and paste one or more junk emails in place (Ctrl+C,V) or move mail into the folder you are watching. Or add the macro below at the end of ThisOutlookSession, then select a message and run the macro.

Sub RunScript()
  Dim objApp As Outlook.Application
  Dim objItem As MailItem
  Set objApp = Application
  Set objItem = objApp.ActiveExplorer.Selection.Item(1)

   'macro name you want to run goes here
    objItems_ItemAdd objItem
End Sub

 
To run either ItemAdd macro on all messages in any folder, use this macro call to call the ItemAdd macro.

Public Sub RunonAnyFolder()
    Dim objOL As Outlook.Application
    Dim objItems As Outlook.Items
    Dim objFolder As Outlook.MAPIFolder
    Dim obj As Object
    Dim i As Long
 
    Set objOL = Outlook.Application
    Set objFolder = objOL.ActiveExplorer.CurrentFolder
    Set objItems = objFolder.Items
 
    For i = objItems.Count To 1 Step -1
    Set obj = objItems(i)
     'macro name you want to run goes here
     objItems_ItemAdd obj
    Next
 
    Set obj = Nothing
    Set objItems = Nothing
    Set objFolder = Nothing
    Set objOL = Nothing
End Sub

Clean up the Junk Email folder was last modified: January 14th, 2020 by Diane Poremsky

Related Posts:

  • How to use an ItemAdd Macro
  • How to Process Mail After Business Hours
  • Process Mail that was Auto Forwarded by a Rule
  • Macro to file Outlook email by sender's display name

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
2 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Amy Louise Jenkins
November 16, 2023 10:27 pm

I would like to write a macro or procedure that deletes the entire spam folder, and have it run at scheduled intervals automatically during the day. It is possible to schedule macros to run at certain times?

0
0
Reply
Retired Geek
December 17, 2019 12:40 am

Thanks. I used the steps you outlined to create and run Rules on only the Junk folder and can now quickly clear out
80-90% of what is clearly Junk with just a few mouse clicks. I'm fine tuning the rules to get even more. I also added via forms a column to see the real email address of the sender (when possible) to further identity parameters for my Junk rules. Still work in progress but a quantum leap for me. Thanks again.

1
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 34

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Use Classic Outlook, not New Outlook
  • Mail Templates in Outlook for Windows (and Web)
  • How to Remove the Primary Account from Outlook
  • Reset the New Outlook Profile
  • Disable "Always ask before opening" Dialog
  • Adjusting Outlook's Zoom Setting in Email
  • How to Hide or Delete Outlook's Default Folders
  • This operation has been cancelled due to restrictions
  • Shared Mailboxes and the Default 'Send From' Account
  • Change Outlook's Programmatic Access Options
  • Opt out of Microsoft 365 Companion Apps
  • Mail Templates in Outlook for Windows (and Web)
  • Urban legend: Microsoft Deletes Old Outlook.com Messages
  • Buttons in the New Message Notifications
  • Move Deleted Items to Another Folder Automatically
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Opt out of Microsoft 365 Companion Apps

Mail Templates in Outlook for Windows (and Web)

Urban legend: Microsoft Deletes Old Outlook.com Messages

Buttons in the New Message Notifications

Move Deleted Items to Another Folder Automatically

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

:wpds_smile::wpds_grin::wpds_wink::wpds_mrgreen::wpds_neutral::wpds_twisted::wpds_arrow::wpds_shock::wpds_unamused::wpds_cool::wpds_evil::wpds_oops::wpds_razz::wpds_roll::wpds_cry::wpds_eek::wpds_lol::wpds_mad::wpds_sad::wpds_exclamation::wpds_question::wpds_idea::wpds_hmm::wpds_beg::wpds_whew::wpds_chuckle::wpds_silly::wpds_envy::wpds_shutmouth:
wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment