9

I have 1 million mails generated in a Linux sever 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 1 month's mail and remove the remaining.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
ksk.eaknath
  • 91
  • 1
  • 1
  • 2
  • 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 '14 at 10:55
  • I am accessing though mail command. I need to delete the mails whichever more than 1 month old. – ksk.eaknath Aug 25 '14 at 13:16

4 Answers4

7

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 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. – ksk.eaknath Aug 26 '14 at 09:33
5

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.

abchk1234
  • 355
  • 1
  • 6
  • 13
  • 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 '18 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 '18 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 '18 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: https://sourceforge.net/p/archivemail/code/ci/master/tree/archivemail (near the end, in function *check_sane_destdir*). – abchk1234 Jan 26 '18 at 15:08
  • 1
    under a legacy `rpm` based Linux I was able to create an `rpm` for `archivemail` using https://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 – Stuart Cardall Aug 14 '18 at 11:40
3

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)

jesse_b
  • 35,934
  • 12
  • 91
  • 140
StuWhitby
  • 67
  • 4
0

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

Jan
  • 7,600
  • 2
  • 34
  • 41