Page 1 of 1

[Ticket ID: 50488752] Clear Mail By Cron

Posted: Mon May 11, 2015 4:53 pm
by Scott
Here I’m going to list the best way to clear mail from a domains mail box which is older than a set period. Support Ticket ID:50488752 requests a way to clear their mail which is older than 15 days via a cron job. Here’s how you would do this using the +mtime handle.

First off we need to create a file anywhere on the server if your using SSH or using notepad on your PC, as I’m completing this ticket whilst also producing this guide I’m going to be using SSH. cPanel uses three folders to manage mail these are;

New: This is where all mail is first received
Cur: Once opened mail is dropped into here
Tmp: This folder contains processed mail for delivery

The default location for these files would be /home/cPanelusername/mail/

NOTE: emailbox represents the mail user folder. (emailbox/ for

Let’s first create a file we can work inside

Code: Select all


now let’s edit the file

Code: Select all


Next add in the below line to the file

Code: Select all

find /home/cPanelusername/mail/ -type f -mtime +15 -exec rm -f {} ;

Save and close the file CTRL + X

Code: Select all

chmod 755 /root/

That’s our file now created. Next we need to add a cron job to run this file, I’m thinking once a day is fine for this script. You could even run it once a week if needed.

If you’re a Reseller your not likely going to have access to the root crontab so the file we just created would need to be placed into your cPanel home directory (/home/cPanelusername) and then create a cron job from your cPanel account like below

Code: Select all

0 0 1 * * /home/cPanelusername/ > /dev/null 2>&1

VPS and Dedicated users can add the cron to root by issuing the below command

Code: Select all

Crontab –e 

Code: Select all

0 0 1 * * /root/ > /dev/null 2>&1