229

I have setup some cron jobs and they send the crons result to an email. Now over the months I have accumulated a huge number of emails.

Now my question is how can I purge all those emails from my mailbox?

Ross Rogers
  • 23,523
  • 27
  • 108
  • 164
anjan
  • 3,147
  • 6
  • 26
  • 31

8 Answers8

486

alternative way:

mail -N
d *
quit

-N Inhibits the initial display of message headers when reading mail or editing a mail folder.
d * delete all mails

petermeissner
  • 12,234
  • 5
  • 63
  • 63
timaschew
  • 16,254
  • 6
  • 61
  • 78
180

You can simply delete the /var/mail/username file to delete all emails for a specific user. Also, emails that are outgoing but have not yet been sent will be stored in /var/spool/mqueue.

EdoDodo
  • 8,220
  • 3
  • 24
  • 30
  • The email at question which receive all cron emails is say cron_results@site.com. I just want to purge all the emails received on this email, leaving others intact. deleting the /var/www/username will delete all, right? – anjan Aug 16 '11 at 09:43
  • 3
    `username` has to be replaced with the user for which you'd like to remove the emails. In your case, the emails are being sent to the user called `cron_results`, so you would have to delete `/var/www/cron_results`. – EdoDodo Aug 16 '11 at 09:51
  • 7
    It is not a good praxis data manipulation from outside an application. If there is an option or command that can do the job, it is better to use it. As @timaschew answered, you can use the ‘d’ command inside the mail tool. – pocjoc Sep 03 '15 at 11:29
  • 1
    Very practical when 'mail' only responds with 'Not enough memory - Aborted'. – Herbert Van-Vliet Nov 20 '18 at 10:19
  • Wow my mailbox was 36 GB (cronjob logs). Thanks that saved me a lot of time. – Harald Ernst Dec 03 '21 at 10:15
  • Another way is to type `> /var/spool/mail/root` – Robin Jan 30 '23 at 03:53
75

Just use:

mail
d 1-15
quit

Which will delete all messages between number 1 and 15. to delete all, use the d *.

I just used this myself on ubuntu 12.04.4, and it worked like a charm.

For example:

eric@dev ~ $ mail
Heirloom Mail version 12.4 7/29/08.  Type ? for help.
"/var/spool/mail/eric": 2 messages 2 new
>N  1 Cron Daemon           Tue Jul 29 17:43  23/1016  "Cron <eric@ip-10-0-1-51> /usr/bin/php /var/www/sandbox/eric/c"
 N  2 Cron Daemon           Tue Jul 29 17:44  23/1016  "Cron <eric@ip-10-0-1-51> /usr/bin/php /var/www/sandbox/eric/c"
& d *
& quit

Then check your mail again:

eric@dev ~ $ mail
No mail for eric
eric@dev ~ $

What is tripping you up is you are using x or exit to quit which rolls back the changes during that session.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Mip
  • 751
  • 5
  • 2
28

One liner:

echo 'd *' | mail -N
Michael Munsey
  • 3,740
  • 1
  • 25
  • 15
26

Rather than deleting, I think we can nullify the file, because the file will be created if the mail service is still on. Something like following will do the job

cat /dev/null >/var/spool/mail/tomlinuxusr

And yes, sorry for awakening this old thread but I felt I could contribute.

xploreraj
  • 3,792
  • 12
  • 33
  • 51
9

On UNIX / Linux / Mac OS X you can copy and override files, can't you? So how about this solution:

cp /dev/null /var/mail/root
peter_pilgrim
  • 1,160
  • 11
  • 18
2

If you're using cyrus/sasl/imap on your mailserver, then one fast and efficient way to purge everything in a mailbox that is older then number of days specified is to use cyrus/imap ipurge command. For example, here is an example removing everything (be carefull!!), older then 30 days from user vleo. Notice, that you must be logged in as cyrus (imap mail administrator) user:

[cyrus@mailserver ~]$ /usr/lib/cyrus-imapd/ipurge -f -d 30 user.vleo Working on user.vleo... total messages 4 total bytes 113183 Deleted messages 0 Deleted bytes 0 Remaining messages 4 Remaining bytes 113183

vleo
  • 351
  • 3
  • 4
1

Rather than use "d", why not "p". I am not sure if the "p *" will work. I didn't try that. You can; however use the following script"

#!/bin/bash
#

MAIL_INDEX=$(printf 'h a\nq\n' | mail | egrep -o '[0-9]* unread' | awk '{print $1}')

markAllRead=
for (( i=1; i<=$MAIL_INDEX; i++ ))
do
   markAllRead=$markAllRead"p $i\n"
done
markAllRead=$markAllRead"q\n"
printf "$markAllRead" | mail
WSimpson
  • 460
  • 4
  • 7