Some automatic processes, like those made at regular times using cron, generate a lot of messages. I would like to delete automatically those older than a month so my mailbox doesn't grow too much.

I tried with a bash script that archives the old messages and tries to delete them from the mailbox, but I get this message from the system:

Mailbox /var/spool/mail/username was modified or is in use now.
It's not safe to complete the operation...

I have the same problem with both exim and postfix (in different servers).

I guess I could do it simulating a local email client like mutt using bash or python but I am not sure how to do it. I am not interested in using maildir because there are thousands of small messages that are more manageable in a single mailbox.

This is a local server using mail spool and local clients. We are not using imap or sending messages outside the server.

I will appreciate any hints.

  • 1
    This isn't typically something for the MTA to handle. What MDA are you using?
    – Paul
    Commented Aug 19, 2022 at 15:47
  • 1
    Actually I am not using a MDA since the mail is local and mutt can read it directly from the spool. Commented Aug 19, 2022 at 17:16

1 Answer 1


The grepmail command can do what you want. You may also be able to accomplish this using just the mail[x] command with some ingenuity. I would suggest something like:

mailx <<EOF
s 1-$ mbox

grepmail ...

This will save all messages in your system mailbox to a file called mbox. It will also delete those messages from your system mailbox. You can then run grepmail on the mbox file.

  • Thanks, the problem is that I need to do this automatically from a script. If I put your mailx commands in a bash script it doesn't recognize s as a command but as a syntax error , and fails. Commented Aug 20, 2022 at 1:10

You must log in to answer this question.

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