Cronjob to delete all emails older than one month from server.

Is there any way to remove old emails automatically from server?

Before starting with the cronjob script, we must know the locations where the emails stored in the server. Then we can go to the steps to remove them 😉 Exactly there are three folders in a cPanel based server for managing received emails.

Those are cur, new, tmp. The main differences between these three are listed below:

new : This is the folder where all emails are first received.
cur : Is the folder having emails that are opened/read by mail client.
tmp : Is the folder contains processed emails for delivery purpose.

In most cases these emails in the server cause high disk space usage in the server. We can remove older email by using different ways. Here I’m explaining the steps to remove emails older than one month from the server by the help of cron.

To clear old emails from the server means, remove email related files from cur folder under mails directory and all other mail user directory. There are cur and new directories under the mail folder for cPanel default mail and also the same in each users domain directory. ie /home/username/mail/domain.com/***/cur Here *** represents each mail user folder. (admin/ for admin@domain.com)

Step 1 : Creating an executable file with command to remove emails

For simplicity, create an executable file under anywhere (here I selected the location under root) and place the script to remove older mails.

Here I’m using the find command to find and remove mails which are older than 30 days (-mtime switch for finding mails older than 30 days.

For more about find command please refer this, >> Find command usage with example <<

[root@EcLinux]# touch mailcleaner.sh

Edit the file using your favorite file editor

[root@EcLinux]# vim mailcleaner.sh

find /home/username/mail/yourdomain.com/*/cur -type f -mtime +30 -exec rm -f {} \;
~
 :wq

Then change that file to an executable one. (ie file permission to 755)

[root@EcLinux]# chmod 755 /root/mailcleaner.sh

Step 2 : Creating CRON job

Next step is to set the cronjob under root (for setting a cronjob refer this link). Here I’m setting the cron job to run every 1 day of the month.

For more commands about cronjob, please refer this >> Job scheduling using Cron <<

[root@EcLinux]# crontab -e
 0 0 1 * * /root/mailcleaner.sh > /dev/null 2>&1
 :wq
[root@EcLinux]# crontab -l : To list all cronjobs.

In this case the file mailcleaner.sh will execute first day of every month and remove all emails older than 30days.
Thank you.

Related

1, Simple script to list dedicated IP address usage of domains under a Reseller – WHM/cPanel
2, Keep a log for total number of connections to a particular port from Foreign Address

Arunlal Ashok

Linux Server Administrator. I'm managing Linux servers since 2012. I started this blog to share and discuss my ideas. Any questions? Write to arun (@) crybit.com

You may also like...

12 Responses

  1. Kishore says:

    Hi Arun,

    Thank you for posting these useful documents.

    Regards,
    Kishore.

  2. Ramon Fincken says:

    Many thanks Arun!

    For some reason (Centos) the exec did not work, but this part did:
    -exec rm {} \;

  3. cloudssh says:

    thank you for sharing

  4. Alfa says:

    Good information, Arun.

  5. Chad says:

    Is it safe to use two wildcards if you wanted to do this with all domains hosted on the server?

    find /home/username/mail/*/*/cur -type f -mtime +30 -exec rm -f {} \;

  6. hashim says:

    I need to copy all the cur folder into one backup directory called /backup and it should name as cur1,cur2

    Is that possible?

Leave a Reply

Your email address will not be published. Required fields are marked *