Can I use cron to delete old spam and trashed mail?

I have several users that don’t seem willing/able to clear out their Spam and Trash folders. Most access their email via POP and so their inboxes are being cleared, but their Spam folders now have several months’ worth of spam.

I would like to run these this bash script once per day, using crontab.

#!/bin/bash
cd /home/user-data/mail/mailboxes
find -wholename "./*/*/.Spam/*/*.box.example.com*" -mtime +60 -exec rm {} \;
find -wholename "./*/*/.Trash/*/*.box.example.com*" -mtime +60 -exec rm {} \;

The goal is to delete any emails in Spam and Trash folders that are older than 60 days. Are there any better ways of doing this? Are there any dangers to doing this?

Thanks in advance.

2 Likes

did you implement this?

I did. It seems to work. Just make sure to replace “box.example.com” with your actual box domain name.

1 Like

One more questions, what is the bash script to delete all emails (inbox, sent, draft, trash, spam, etc.), older than 90days?

Is this the correct one?

#!/bin/bash
cd /home/user-data/mail/mailboxes
find -wholename "./*/*/*/*/*.box.example.com*" -mtime +90 -exec rm {} \;