how to auto delete all emails older than 2 years (cpanel)? Any scripts?

ramystyle

Well-Known Member
Feb 9, 2004
67
0
156
Montreal
Hello,

I remember there used to be an Email Aging option in cPanel that is no longer available. I'm looking for something similar that I can apply to all accounts.

Basically, I would like to be able to delete all emails on the server that are older than 2 years.

Any scripts or way of doing this ?

Thank you.
 

Spiral

BANNED
Jun 24, 2005
2,020
8
193
Basically, I would like to be able to delete all emails on the server that are older than 2 years.

Any scripts or way of doing this ?
You can simply run a find against all the standard mail folders
for mail that is older than two years:
Code:
 find -P /home/*/mail/*/*/cur -mtime '+729'
 find -P /home/*/mail/*/*/new -mtime '+729'
The above will give you a list of all the mail files older than two years
and you can either pipe that into 'xargs' for a very quick and dirty way
of doing that pipe into a file and then have a script parse the file to
do whatever you would like. Here is a very basic example:

Code:
#!/bin/bash
IFS="$"

cd /home

find -P /home/*/mail/*/*/* -mindepth 1 -maxdepth 1 -mtime '+729' | while read OLDMAIL; do
  echo "Now deleting ${OLDMAIL} ..."
  rm -f "${OLDMAIL}"
done
(Incidentally the 729 is the number of days old the file must be greater)

You could delete the "echo" line and add this basic code to a cronjob
and have your server auto delete all mail older than 2 years or
whatever length of time that you want.
 
Last edited: