Automatic deletion of emails with cron

iorbita

Registered
Jun 4, 2018
3
0
1
Pescara
cPanel Access Level
Website Owner
Hello,
I have to delete emails older than 90 days with a specific email subject.

Another solution would be to target emails that were previously moved to a specific folder, in this case the idea would be to target the folder concerned.

The cron script that is proposed in this thread seems to suit me, it’s just missing the specific subject to target or, in the other case, the specific folder to target.

Do you have a script to advise me? Many thanks.

Lorenzo
 

cPanelLauren

Forums Analyst II
Staff member
Nov 14, 2017
8,080
657
263
Houston
cPanel Access Level
DataCenter Provider
Hi @iorbita

Looking at that command it does indicate the specific folder that you'd be targeting:

Code:
/home/SERVER_LOGIN_USERNAME/mail/DOMAIN_NAME/MAIL_ACCOUNT_NAME/cur
It's removing mail from cur. You can change that to any one of the folders that are present in the mail directory for the domain for example:

Code:
find /home/%user/mail/example.com/mailuser/new -type f -mtime +90 -exec rm {} \;
This would remove all mail older than 90 days in /home/$user/mail/example.com/mailuser/new

Thanks!
 

spinge

Registered
Jul 16, 2018
1
1
3
Mumbai
cPanel Access Level
Root Administrator
I don't know how to do with a specific subject, but here is the solution to delete Inbox emails older than the last 90 days on a daily rotating basis, and get Cpanel to also update your mailbox size (This does not happen automatically after just deleting the emails).

Assumption:
Let's assume that your email is [email protected] and your cpanel account is CPANELACCOUNTNAME

Solution:
Go to Cpanel > Advanced > Cron Jobs > Add New Cron Job >

a. Common Settings:
Once Per Day (0 0 * * *)

b. Command:
Code:
find /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/ -type d \( -name cur -o -name new \) -exec find {} -mtime +90 -type f -delete \; -o -name maildirsize -delete
c. Click on 'Add new Cron Job'.

Done!

---
Similarly, to delete Trash emails older than 7 days on a daily rotating basis for the same user:

Solution:
Go to Cpanel > Advanced > Cron Jobs > Add New Cron Job >

a. Common Settings:
Once Per Day (0 0 * * *)

b. Command:
Code:
find /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Trash/ -type d \( -name cur -o -name new \) -exec find {} -mtime +7 -type f -delete \;
c. Click on 'Add new Cron Job'.

Done!
---

Note:
mtime +90 = 90 days
mtime +7 = 7 days

Folder names and location for your reference:
Inbox = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/
Trash = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Trash/
Spam = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.spam/
Junk = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Junk/
Sent = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Sent/
Draft = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.Draft/
 
  • Like
Reactions: iorbita