Auto deletion of mails in a mailbox based on age

ebizindia

Well-Known Member
Oct 13, 2005
105
4
168
Kolkata, India
cPanel Access Level
Root Administrator
Hi

I need to auto delete mails older than a certain period (say 30 days) in a mailbox. I am not talking about the mail queue please.

Why? This is an archival mailbox where we collect auto forwarded mails and while we need to be able to see mails sent and received with a 30 day window, older mails should be deleted to conserve space.

I am fine if this can be achieved through some settings or even a cron script.

Please advise.

TIA
 

ebizindia

Well-Known Member
Oct 13, 2005
105
4
168
Kolkata, India
cPanel Access Level
Root Administrator
1. I cannot see a way to delete inbox mails. The auto archived mails will be in inbox.

2. I will not be logging to this account (possibly never until I need to review somebody's emails). So the auto deletion has to be independent of that.
 

kdean

Well-Known Member
Oct 19, 2012
369
61
78
Orlando, FL
cPanel Access Level
Root Administrator
If you set some crons replacing the path with your path to the mailbox folders, the following should delete older than 30 days emails

New folder:

/bin/find /home/account/mail/domain.com/user/new ! -mtime -30 -type f -exec rm {} \;

and if you look at the mail folder periodically, some may be moved to the cur folder even if they're not read:

/bin/find /home/account/mail/domain.com/user/cur ! -mtime -30 -type f -exec rm {} \;

Not sure if this cron requires running as root, so if you can't run it under the user, place with the proper cron syntax in:

/var/spool/cron/root

such as running one at 2:15 am every night:

15 2 * * * /bin/find /home/account/mail/domain.com/user/new ! -mtime -30 -type f -exec rm {} \;
 

ebizindia

Well-Known Member
Oct 13, 2005
105
4
168
Kolkata, India
cPanel Access Level
Root Administrator
After a bit of testing, I figured out the following to achieve this:

/bin/find /home/account/mail/domain/user/new -type f -mtime -30 -exec rm {} \;
/bin/find /home/account/mail/domain/user/cur -type f -mtime -30 -exec rm {} \;

Do you see any issues with this?

What was the ! in your command for? Also I think Linux wanted the /type f first and then mtime.
 

kdean

Well-Known Member
Oct 19, 2012
369
61
78
Orlando, FL
cPanel Access Level
Root Administrator
The ! is important. ! in programming generally refers to NOT.

So...

-mtime -30

refers to within the last 30 days, while....

! -mtime -30

refers to not within the last 30 days or older than 30 days.

Not sure why your Linux would want type in a different order but it really shouldn't matter, but UNIX can be an odd one at times.
 

webstyler

Well-Known Member
Nov 20, 2003
450
0
166
Hello
in the last release (11.38) there is any tools for set auto delete of old message in mailboxes ?
thanks
 

quietFinn

Well-Known Member
Feb 4, 2006
1,281
115
193
Finland
cPanel Access Level
Root Administrator

konrath

Well-Known Member
May 3, 2005
366
0
166
Brasil
The best way to remove emails ( 60 days old )



find /home/a*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/b*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/c*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/d*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/e*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/f*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/g*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/h*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/i*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/j*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/k*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/l*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/m*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/n*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/o*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/p*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/q*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/r*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/s*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/t*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/u*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/v*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/x*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/y*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/w*/mail/*/ -type f -mtime +60 | xargs rm -f
find /home/z*/mail/*/ -type f -mtime +60 | xargs rm -f