Use a Rule to delete older messages as new ones arrive

Last reviewed on April 17, 2014   —  5 comments

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

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

Written by

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.

Please post long or more complicated questions at Outlookforums.

5 responses to “Use a Rule to delete older messages as new ones arrive”

  1. M.Sornamuthu

    Dear Ms Diane Poremsky,

    Thanks for the above code

    Can you please give me the code which can run in all sub folders

    thanks

    M.Sornamuthu

  2. Netpilot

    Hi Diane,

    This script is SO close to what I've been trying to figure out how to do with new RSS items from a VBA script!

    We all know of the problem that Outlook has with receiving duplicate items from certain RSS feeds, depending on how the RSS server is configured. A new duplicate RSS item appears to have the same "From", "Subject", and date "Received" values, but has a newer date "Modified" value.

    When a new RSS item arrives, I'd like a VBA macro to delete all items in that folder which have the same From, Subject, and date Received values, but an older date Modified.

    I have had two problems figuring out how to do this:

    1) RSS items go into their own folder - this script assumes the item will be in the Inbox. Is there a way to trigger a script that processes only items in an RSS folder when a new item arrives?

    2) The script you wrote seems to iterate through and compare the new message to all of the previous items in the folder. With hundreds, or possibly thousands, of items in a folder, that could take a long time, especially when executing in real-time while new items are being received. Is there a way in Outlook VBA to iterate through the items in, say, date Received order so the iteration could stop when the compared date Received is older than the date Received of the new message?

    You would be a hero to many, many people if you came up with a script to solve this problem!

    Thanks in advance.

Leave a Reply

If the Post Coment button disappears, press your Tab key.