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?
Join Stack Overflow to learn, share knowledge, and build your career.
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?
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
.
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 9:51
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
> mbox
in home directory, this just truncates mbox file.
– Ciantic
May 26 '14 at 15:48
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.
exit
rather than quit
was tripping me up. exit
will abort the session, so any emails pending deletion are left intact. quit
will write the changes back, so deleted emails are committed at that point.
– Jason
Jul 8 '15 at 10:27
One liner:
echo 'd *' | mail -N
yes 'd' | mail
works as expected.
– Byoungchan Lee
Jul 30 '18 at 3:31
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.
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
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
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