Deleting Emails from Mail on a Schedule

I have an automation where, when I receive a receipt for my online grocery order, Mail filters the email into a particular folder, and with a combination of Automator and Hazel the pdf receipt is removed from the email and added to a folder where I keep electronic receipts for processing later.

The automation is incomplete because I cannot work out how to then delete the email that contained the receipt pdf without manually doing it. I run this automation on a schedule each Friday around 5pm.

Can anybody help?

Thanks, Brian

I’ve been looking for a way to do something similar: I get a lot of emails to one account from merchants, most of which a rent receipts but marketing offers of some kind.

I usually unsubscribe, but some I want to keep around, just in case. But they all have limited lives (3-5 days usually). So they go into a smart folder names for the appropriate length… and there they sit until I clear out the folders, because I can figure out a way to automatically delete an email X days after receipt.

It seems like such a basic need, but I don’t think even Sanebox does that (though I could be wrong – I prefer not to use services that require me to send email to someone else’s server).

Hi Brian

You could use an AppleScript to move the messages in a specific mailbox to the deleted messages mailbox.

The script can be modified for tf2 so that only messages older than n days are selected using “date received < t”, where t is current date - (days * n).

1 Like

Thank you very much for doing this for me. :slight_smile:

I will give it a go :grinning:

Sorry, I’ve looked online but can’t find the Apple Script you mentioned. Do you have a link?

Thanks, Brian

Hi Brian

You can delete multiple messages (including selecting by date received) like this:

tell application "Mail"
	set targetMailbox to mailbox "Receipts"
	set mailBin to mailbox "Deleted Messages"
	set selectMessages to messages of targetMailbox whose date received < ((current date) - (days * 365))
	repeat with m in selectMessages
		move m to mailBin
	end repeat
end tell

As your automation will have already selected a single message, you’d just a single line of AppleScript to move it to your Deleted Messages mailbox.


This discussion prompted me to create a workflow to process images coming in from my security cameras.

I expanded orpy’s example to move the Mail message to a Mailbox, copy the Attachment to a Finder Folder and cull Messages older than 7 days.

It is easily adaptable for other purposes, I have for example made a version to capture all PDFs received to a central folder.

Code can be found here -



1 Like

Thank you very much for doing this for me. :slight_smile:

I have set a test so that the message I am testing with should move in 2 days to the Deleted file.

May I let you know how I get on?

Thanks, Brian

Hi Iain

Thank you very much for doing this for me. :slight_smile:

Sadly, I am not a programmer and although I can edit very simple code, I wasn’t sure what to do with the code you created!

Thanks so much though for responding to me. I am trying to create more ‘end to end’ automations and this posting was about that. I had managed to create an automation that got me 90% there but couldn’t do the final part.

All good wishes,


Hi Brian

Yes, let me know how you get on.

I couldn’t wait two days to see if a script works :slight_smile:

If I were you, I’d create a test mailbox and drag a test message in to it so you can test your script.

If you run into problems you can always select the test message and run a single line, like:

tell application "Mail" to get selection

--> {message id 252052 of mailbox "Suppliers/AcmeCo" of application "Mail"}

So you can make sure that the script is targeting the correct messages/mailbox/account etc.

Adding more test messages with a range of dates to the test mailbox will then allow you to test if selectMessages date selection is working properly.

Just comment out the repeat part of the script while you’re testing so that the messages stay where they are.

Once you’re satisfied that the list of messages returned by the script is exactly as you want you can then let the repeat run with confidence that it isn’t going to do anything unexpected.

Good luck.


I updated the code to give a description and purpose of the configurations you need to set. The rest of the code you don’t need to touch.

To test the delete function just grab some Junk mail older then the number of days you set in the configuration and place them into the folder you are moving the mail to, then run the script.

I am running the Ventura Beta and I am finding the mail rules are not always being triggered. An option I will try is to create a repeating Calendar to trigger the Applescript.

If you are still stuck drop me a line and maybe we can jump on a Zoom and I can help you out.


Hi Iain - yes, I am stuck!

If we could have a chat, I would be most welcome.

I am in the UK.

Thanks so much, Brian