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

Use a Rule to delete older messages as new ones arrive

Slipstick Systems

› Developer › Use a Rule to delete older messages as new ones arrive

Last reviewed on August 29, 2018     48 Comments

A security update disabled the Run a script option in the rules wizard in Outlook 2010 and all newer Outlook versions. See Run-a-Script Rules Missing in Outlook for more information and the registry key to fix restore it.

I want to delete an older email when a new message comes in (they have the same subject line).

This run a script sample is perfect to use with status reports and similar messages where you really only need to keep the most recent copy. For it to work, the messages need to have the same subject line (and the subject should be unique, because all messages matching the condition will be deleted.)

To use, create a Rule that checks the message subject and choose Run a Script as the action, selecting this script. The macro checks the Item.Subject, so if you need to run it on messages with different subjects you can use one rule.

While it only runs if Outlook is open, old messages won't pile up, as it deletes all older messages that meet the conditions when Outlook is open.

Use a script to delete older messages

See Outlook's Rules and Alerts: Run a Script for more information on using Run A Script rules.

Sub DeleteOlderMessages(Item As Outlook.MailItem)

Dim objInbox As Outlook.MAPIFolder
Dim intCount As Integer
Dim objVariant As Variant

Set objInbox = Session.GetDefaultFolder(olFolderInbox)

' Remove these lines if you don't want to add a category
Item.Categories = "Delete Older"
Item.Save

For intCount = objInbox.Items.Count To 1 Step -1
 Set objVariant = objInbox.Items.Item(intCount)
 If objVariant.MessageClass = "IPM.Note" Then
    If objVariant.Subject = Item.Subject And objVariant.SentOn < Item.SentOn Then
     objVariant.Delete
     Else
    End If
 End If
Next

Set objInbox = Nothing
End Sub

 

ItemAdd macro version

This version of the macro watches a specific folder for new items, in this example it is watching a subfolder under the Inbox. See Working with VBA and non-default Outlook Folders for code samples to use other folders.

This macro goes into ThisOutlookSession.

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(olFolderInbox).Folders("My Folder")
Set objItems = objWatchFolder.items

Set objWatchFolder = Nothing
End Sub

Private Sub objItems_ItemAdd(ByVal Item As Object)
Dim intCount As Integer
Dim objVariant As Variant

For intCount = objItems.Count To 1 Step -1
 Set objVariant = objItems.Item(intCount)
   If objVariant.Subject = Item.Subject And objVariant.SentOn < Item.SentOn Then
     objVariant.Delete
     Else
    End If
Next
End Sub

How to use macros

First: You will need macro security set to low during testing.

To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s 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.

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

To put the code in a module:

  1. Right click on Project1 and choose Insert > Module
  2. Copy and paste the macro into the new module.

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

More Information

More Run a Script Samples:

  • Autoaccept a Meeting Request using Rules
  • Automatically Add a Category to Accepted Meetings
  • Blocking Mail From New Top-Level Domains
  • Convert RTF Messages to Plain Text Format
  • Create a rule to delete mail after a number of days
  • Create a Task from an Email using a Rule
  • Create an Outlook Appointment from a Message
  • Create Appointment From Email Automatically
  • Delegates, Meeting Requests, and Rules
  • Delete attachments from messages
  • Forward meeting details to another address
  • How to Change the Font used for Outlook's RSS Feeds
  • How to Process Mail After Business Hours
  • Keep Canceled Meetings on Outlook's Calendar
  • Macro to Print Outlook email attachments as they arrive
  • Move messages CC'd to an address
  • Open All Hyperlinks in an Outlook Email Message
  • Outlook AutoReplies: One Script, Many Responses
  • Outlook's Rules and Alerts: Run a Script
  • Process messages received on a day of the week
  • Read Outlook Messages using Plain Text
  • Receive a Reminder When a Message Doesn't Arrive?
  • Run a script rule: Autoreply using a template
  • Run a script rule: Reply to a message
  • Run a Script Rule: Send a New Message when a Message Arrives
  • Run Rules Now using a Macro
  • Run-a-Script Rules Missing in Outlook
  • Save all incoming messages to the hard drive
  • Save and Rename Outlook Email Attachments
  • Save Attachments to the Hard Drive
  • Save Outlook Email as a PDF
  • Sort messages by Sender domain
  • Talking Reminders
  • To create a rule with wildcards
  • Use a Macro to Copy Data in an Email to Excel
  • Use a Rule to delete older messages as new ones arrive
  • Use a run a script rule to mark messages read
  • Use VBA to move messages with attachments
Use a Rule to delete older messages as new ones arrive was last modified: August 29th, 2018 by Diane Poremsky

Related Posts:

  • Receive a Reminder When a Message Doesn't Arrive?
  • A simple run a rule script marks Outlook messages read when the messag
    Use a run a script rule to mark messages read
  • Convert RTF Messages to Plain Text Format
  • Creating an AND rule in Outlook Rules

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
48 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Carly (@guest_221064)
May 2, 2024 10:15 pm
#221064

Is there a video on how to do this?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Carly
May 14, 2024 11:36 am
#221084

No, but I will make one.

1
0
Reply
Steven (@guest_220436)
July 24, 2023 5:00 am
#220436

Can this be done without the user of macros?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Steven
August 15, 2023 7:55 am
#220496

No, rules don't support this on their own.

0
0
Reply
Mark (@guest_220048)
February 3, 2023 1:33 pm
#220048

This is the only working solution I have found so far without installing any add ons! thanks!. Would it be possible to delete the new ones instead of keeping the latest?

0
0
Reply
J.A. Clark (@guest_219475)
June 26, 2022 5:47 pm
#219475

Hello - Appreciate all the macros and the work it took to compile all these. Nice that these macros work after 5+ years of being posted:-)

I have implemented the version called by an Outlook rule as well as the ItemAdd macro version and both work until they encounter an email that has the same date and time (which occasionally happens when I receive emails generated by an inhouse application). I've tried to modify the if statement controlling what emails are deleted based on the subject and time and can't seem to find the correct combination. What happens is neither email is deleted as neither email would match the if statement.

How can the macro (preferably the macro run by a rule) delete one of the matching emails with the same date and time?

Any assistance would be appreciated.

1
0
Reply
Pramod (@guest_210754)
March 30, 2018 4:05 pm
#210754

I m trying to use this script and it doesnt work. my current rule is set to move mails from a group to a folder(say group1) same level as Inbox.

i want to run this rule on group1 and my expectation are , it should delete an older email when a new message comes with the same Subject

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 3, 2018 12:41 am
#210763

Rules only run on the inbox. You can run it on the folder later and it should work. If you want it to work as the messages arrive, you need to move the message using the script - you cant mix actions and scripts.

you'll need to change this line to watch the folder:
Set objInbox = Session.GetDefaultFolder(olFolderInbox).parent.folders("group1")

to move the message, use this after the search:
item.move objInbox

0
0
Reply
Pramod (@guest_210777)
Reply to  Diane Poremsky
April 3, 2018 5:52 pm
#210777

Thanks , I use outlook 2016 . I have another question.

I have a folder called group1 , which has about 500 mails a day .

can you help me with a script which can keep the latest mail based on the subject.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 3, 2018 10:02 pm
#210778

I added an itemadd macro to the page - it will work better with high volume. You can use a rule to move mail into the folder and the itemadd will do the search. If there are a lot of messages in the folder to search, it will be slow - to speed it up, you can limit the search to the newest 50 messages or so.

0
0
Reply
Pramod (@guest_210784)
Reply to  Diane Poremsky
April 4, 2018 11:45 am
#210784

when I run the script it gives me an error script ** doesn't exist or is invalid

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 4, 2018 11:44 pm
#210785

are you using the run a script version or the new itemadd version?

The itemadd macro runs on its own, not in a rule.

0
0
Reply
Pramod (@guest_210788)
Reply to  Diane Poremsky
April 5, 2018 3:45 pm
#210788

I did a copy / paste of your Code "ItemAdd macro version" using ALT + F11 and saved them under "This OutlookSession" but I don't see any mails getting deleted. Do I need to do something else to make this work.

when I try to add it in rule I get the error mentioned in the previous post.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 6, 2018 12:01 am
#210797

you either need to restart outlook or click in the application_startup macro and press Run.

0
0
Reply
Pramod (@guest_210789)
Reply to  Diane Poremsky
April 5, 2018 4:00 pm
#210789

Just to add, when I restart outlook

run-time error '-2147221233(8004010f)': The attempted operation failed. An object could not be found.

Debug pointing to
Set objWatchFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("My Folder")

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 6, 2018 12:02 am
#210798

Do you have a subfolder of the Inbox called My Folder? If it can't find the folder, it definitely will error.

0
0
Reply
Pramod (@guest_210802)
Reply to  Diane Poremsky
April 6, 2018 1:02 am
#210802

Yes I have them but it doesn't work.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 6, 2018 11:38 am
#210809

if you get errors after restarting outlook... then its not macro security settings.

i don't know what to say - it is working on my test system.

subfolder.png
0
0
Reply
Pramod (@guest_210810)
Reply to  Diane Poremsky
April 6, 2018 3:25 pm
#210810

Yes I fixed it , I added it as parent.folders but its not deleting mails on the folder.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 7, 2018 2:02 pm
#210813

Are the subjects identical and sent at an earlier time? If there are a lot of messages in the folder and high traffic, it can be overwhelming for the macro - you'll need to limit it to like the last 30 or 50.

I think this is how i did it when i had 3000+ messages and needed to limit it to recent messages (that macro is on my office computer) -
For intCount = objItems.Count To objItems.Count - 50 Step -1

0
0
Reply
Pramod (@guest_210816)
Reply to  Diane Poremsky
April 8, 2018 12:47 pm
#210816

Do I need to restart outlook everytime or does it run at the backend always?

Just wanted to thank you for all the help.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 8, 2018 11:50 pm
#210819

it runs in the background - but if you are editing it, you either need to restart outlook or click in the application run macro then click Run to kick start it with the changes.

0
0
Reply
Pramod (@guest_210824)
Reply to  Diane Poremsky
April 9, 2018 11:57 am
#210824

Thanks, Last Query :

Can you do this for 2-3 Folders in Parallel ?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Pramod
April 9, 2018 9:40 pm
#210825

You can - you need to reference each folder in the application startup macro and create an itemadd macro for each. You can do it something like this and to share the macro that does the work -

Private Sub objItems_ItemAdd(ByVal Item As Object)
FindDuplicates item
End sub

Private Sub objItemsFolder_ItemAdd(ByVal Item As Object)
FindDuplicates item
End sub

Private Sub FindDuplicates(ByVal Item As Object)
Dim intCount As Integer
Dim objVariant As Variant

For intCount = objItems.Count To 1 Step -1
Set objVariant = objItems.Item(intCount)
If objVariant.Subject = Item.Subject And objVariant.SentOn < Item.SentOn Then objVariant.Delete Else End If Next End Sub

0
0
Reply
Arun (@guest_210745)
March 30, 2018 1:30 am
#210745

Rule in Outlook: Mail from Public Group Move to Folder say Time.

Need a Script to run on the Folder (Time) which will keep latest mail on a particular Subject and move the remaining one to a new folder(say Time1)

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Arun
April 3, 2018 12:44 am
#210764

This assumes the Time and time1 folders are a subfolder of the inbox:
Set objInbox = Session.GetDefaultFolder(olFolderInbox).folders("Time")
Set objTime1 = Session.GetDefaultFolder(olFolderInbox).folders("Time1")

then change objVariant.Delete to
objVariant.move objTime1

objVariant.Delete

0
0
Reply
Carlo Borreo (@guest_207431)
June 29, 2017 6:06 am
#207431

I am not sure I understand. Is your script:

a) Deleting all emails with the same subject of a newer email, whatever is the subject

OR

b) Deleting all emails with the same specific subject X of a newer email

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Carlo Borreo
July 2, 2017 12:05 am
#207493

It's a run a script rule, so you'd use whatever condition you want to limit the messages that are touched by the script - it can be sender, subject - any available rule condition. If an incoming message meets the conditions in the rule, the macro looks for older messages matching the subject of the new message.

If you want it to apply to all messages, leave the rule conditions blank.

0
0
Reply
Kim (@guest_200271)
July 27, 2016 4:14 pm
#200271

Thanks for all your scripts! Very helpful!

Hoping you can give me some guidance ...

I'm trying to use an existing script but need to make an adjustment or two ... using DeleteOlderMessages ...

I need the script to categorize the incoming email as 'Most Recent' but then change the category to '*Disregard' when it's deleted ...

and I'd rather move that email to another folder instead of deleting it ... i'm having the biggest issue with changing the category upon moving/deleting ...

any suggestions?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Kim
July 28, 2016 1:10 am
#200279

you may need to change it before moving and may need to save the message after setting the category.

' Remove these lines if you don't want to add a category
Item.Categories = "Most Recent"
Item.Save

and

If objVariant.Subject = Item.Subject And objVariant.SentOn < Item.SentOn Then objVariant.categories = "Disregard" objvariant.save ' move code goes here

1
0
Reply
Kim (@guest_200286)
Reply to  Diane Poremsky
July 28, 2016 11:13 am
#200286

Perfect! thanks!

0
0
Reply

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

Latest EMO: Vol. 30 Issue 16

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
  • How to Remove the Primary Account from Outlook
  • Disable "Always ask before opening" Dialog
  • Adjusting Outlook's Zoom Setting in Email
  • This operation has been cancelled due to restrictions
  • How to Hide or Delete Outlook's Default Folders
  • Reset the New Outlook Profile
  • Save Attachments to the Hard Drive
  • Add Attachments and Set Email Fields During a Mail Merge
  • Outlook SecureTemp Files Folder
  • 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
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
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

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

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

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.

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