I have 1 million mails generated in a Linux server due to a cron job. I want to delete the mails, but not all as this cron mails useful to troubleshoot and find out the issue with cron job's execution. So I need to keep one month's mail and remove the remaining.

  • 2
    How is your mail kept on your system? In a single file, in a mail directory, in a database? How do you access it? Directly in the filesystem, through IMAP?
    – lgeorget
    Aug 25, 2014 at 10:55
  • I am accessing though mail command. I need to delete the mails whichever more than 1 month old. Aug 25, 2014 at 13:16

4 Answers 4


There are many different mail commands out there with different command sets. POSIX standardizes a mailx command.

Your mailbox will typically contain emails in the order in which they were received. So to delete older mails, you would typically delete lower-numbered emails. You can delete a range of messages with a command like d 1-42 (delete the first 42 messages).

If you want more control, I recommend Mutt, a command line email client that gives you a lot more control than the mail command. Mutt has a text mode interface and can also be scripted. In Mutt, you can use the command D to mark messages for deletion and enter the pattern ~d ->1m to select messages that are more than 1 month old. If you're satisfied with the list of messages to delete, type x to delete the messages marked for deletion.

  • Thanks a lot Gilles. Yes its working. But Can I have the exact script to delete the mails which are more than one month old with this mutt command. Aug 26, 2014 at 9:33

After a lot of searching I found archivemail using which was able to clear old mails.

The syntax is:

archivemail -d 30 --delete <path-to-mailbox>

(In usual cases $MAIL defines the local mailbox location, try echo $MAIL to check).

You can also check what it is going to do with the -n (--dry-run) option.

  • One annoyance with archivemail, at least on CentOS, is that it refuses to process mail files under /var/spool/mail (ie: the above example with $MAIL doesn't work on CentOS). This is because it insists on being able to write in the directory alongside the mail file (which strictly speaking, it shouldn't need to)... A workaround is to copy the mail file to a temp location for processing and copy it back afterwards; which raises an issue with concurrent modification of the underlying file.
    – sxc731
    Jan 25, 2018 at 12:24
  • 1
    @sxc731 I was able to reproduce the issue (got: archivemail: no write permission on output directory: '/var/mailtest'). Could try filing a bug report. Btw would it be reasonable for you to change permissions for /var/spool/mail? On ubuntu it seems to be drwxrwsrwt.
    – abchk1234
    Jan 25, 2018 at 13:23
  • Hi there, actually on my Ubuntu Xenial (and later) boxes /var/spool/mail is drwxrwsr-x (not sure how you ended up with smth different?). Come to think of it, it's probably fair enough for it not to be world-writable. As for the bug report; it would probably have to be filed against the Python mailbox module...
    – sxc731
    Jan 25, 2018 at 16:04
  • I dunno, on the system I checked somehow the permissions got modified; on another system I too have drwxrwsr-x. I checked the code, and found the error there: sourceforge.net/p/archivemail/code/ci/master/tree/archivemail (near the end, in function check_sane_destdir).
    – abchk1234
    Jan 26, 2018 at 15:08
  • 1
    under a legacy rpm based Linux I was able to create an rpm for archivemail using docs.python.org/2.0/dist/creating-rpms.html - it works perfectly for archiving sent mail from mutt - you need to su to the user who owns the sent file Aug 14, 2018 at 11:40

I've been struggling with this same issue for a while, and Googling for a simple answer was tougher than expected for the mail server I'm administering.

I needed to delete mail from a single user's mailbox which was regularly filling up the filesystem and ended up with the following cron job:

18 5 * * * /usr/bin/mutt -f /var/spool/mail/developer -e "push D~d>7d<enter>qy<enter>"

Or: at 05:18 every day, run mutt on file (directory in this case) /var/spool/mail/developer, wait until mutt is up (push), mark for deletion (D) patterns matching (~) date (d) older than 7 days (>7d), to conclude that command, then quit (q) and confirm deletion (y)


mutt can delete using regular expressions, details here and here.

  • Hi Jan, Yes its working. But Can I have the exact script to delete the mails which are more than one month old with this mutt command. Aug 26, 2014 at 9:37
  • To select messages olther than one month, use ~d >1m
    – Jan
    Aug 26, 2014 at 13:33
  • First link (mutt.org/doc/manual/manual-4.html#ss4.2) is dead. I get a 404.
    – Bulrush
    Aug 8, 2016 at 13:48
  • @Bulrush Thank you, updated the link to point to the new location.
    – Jan
    Aug 9, 2016 at 22:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .