I am new to Unix here.

We have some mailboxes that are taking up an incredible amount of space and I'm trying to figure out a way to delete all mail that has been in the box for 30 days. Most of what I look up, deals with just one mailbox.

I haven't done much in this area yet and any help would be greatly appreciated.

  • To be clear, do you want to totally remove all mail every 30 days (so even mail which arrived 2 seconds ago gets deleted), or do you want to remove items of mail over 30 days old from all mailboxes. – EightBitTony Mar 2 '16 at 14:57
  • I see my miscommunication. I would like to remove mail that has been in the system for 30 days. – N. Hanks Mar 2 '16 at 15:04
  • Next one, can you confirm how mail is stored on the server, is it mbox (traditional, everyone has a single /var/spool/mail/username file), or Maildir (where everyone has a directory, in which individual items of mail are single files). I'm assuming the former, but better to know for definite. Also, which UNIX or Linux distribution? – EightBitTony Mar 2 '16 at 15:20
  • It is the former. As far as the distribution I'm afraid when I try and proc/version it says it's denied...unsure of why. – N. Hanks Mar 2 '16 at 15:32
  • Ah well, archivemail looked useful, but new versions don't allow root to expire other users mail files, which seems a backward step. Do you have an example for one user that you know works, but you just want to use it on all the mail files? – EightBitTony Mar 2 '16 at 15:41

If you want to clear out all of the mailbox contents except maybe root and some other protected user, you can use something like this:

for mbox in $(ls /var/spool/mail/*|grep -v -e root -e protecteduser);do >${mbox};done

and schedule it in cron to run on the 1st day of each month with

crontab -e 

insert the following line at the end of the crontab:

0 2 1 * * /path/to/mailbox/cleaner/script 

this will make the script run on the first day of each month at 2 AM

On the other hand if you need to clean mail which is older than 30 days in each mailbox, it will need a totally different approach. If this is your intention, please update your original post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.