How do I purge a linux mail box with huge number of emails?

EmailPurge

Email Problem Overview


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?

Email Solutions


Solution 1 - Email

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

Solution 2 - Email

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.

Solution 3 - Email

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.

Solution 4 - Email

One liner:

echo 'd *' | mail -N

Solution 5 - Email

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.

Solution 6 - Email

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

Solution 7 - Email

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

Solution 8 - Email

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionanjanView Question on Stackoverflow
Solution 1 - EmailtimaschewView Answer on Stackoverflow
Solution 2 - EmailEdoDodoView Answer on Stackoverflow
Solution 3 - EmailMipView Answer on Stackoverflow
Solution 4 - EmailMichael MunseyView Answer on Stackoverflow
Solution 5 - EmailxplorerajView Answer on Stackoverflow
Solution 6 - Emailpeter_pilgrimView Answer on Stackoverflow
Solution 7 - EmailvleoView Answer on Stackoverflow
Solution 8 - EmailWSimpsonView Answer on Stackoverflow