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

Locked
luxusmind
Posts: 3
Joined: Fri 14. Jun 2019, 17:34

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

Post by luxusmind »

Hello,

First of all Happy holidays to all of you.
I am trying to setup a Cron Job that would delete emails older than one month, but until now i was unsuccessful.

I was using this tutorial while i had my websites hosted on cPanel. And would like to know if that's okay also with KeyHelp or should i do it in another way.

I have posted it instead of pointing to the link.

[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.


[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.

Credits for the tutorial goes to: Crybit
Mit freundlichen Grüßen
Ibrahim
User avatar
Alexander
Keyweb AG
Posts: 3809
Joined: Wed 20. Jan 2016, 02:23

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

Post by Alexander »

Hello,

With KeyHelp, mails are stored at /var/mail/vhosts/<DOMAIN_PART>/<USERNAME_PART>/cur

So you have to update the path you are using in your mailcleaner.sh
Mit freundlichen Grüßen / Best regards
Alexander Mahr

**************************************************************
Keyweb AG - Die Hosting Marke
Neuwerkstr. 45/46, 99084 Erfurt / Germany
http://www.keyweb.de - http://www.keyhelp.de
**************************************************************
luxusmind
Posts: 3
Joined: Fri 14. Jun 2019, 17:34

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

Post by luxusmind »

Alexander wrote: Fri 21. Jun 2019, 08:02 Hello,

With KeyHelp, mails are stored at /var/mail/vhosts/<DOMAIN_PART>/<USERNAME_PART>/cur

So you have to update the path you are using in your mailcleaner.sh
Thank you a lot for your response.
I'll post it here for future references, there may be other users that would surely search for it in future.

If you want to schedule a Cronjob and delete emails that are older than 30 days all you have to do is add the above command into your crontab.
crontab -e is used to edit cronjobs.
Add it anywhere you want, modify the domain.com with your domain name and the username with your desired email address.
* * * * * find /var/mail/vhosts/domain.com/username/cur -type f -mtime +30 -exec rm -f {} \; >/dev/null 2>&1
Thank you again Alexander.
Mit freundlichen Grüßen
Ibrahim
Locked