Is it possible to delete all mail in which the subject matches a regex pattern?

For example, to delete message 1, you do:

d 1

But to delete all mail with subject starting with, say, [SPAM], I can't do:

d -s "^\[SPAM\].*$"
  • 1
    Can you use mutt? That can match-and-then-delete by pattern. – thrig Jan 17 '17 at 18:24
  • 2
    Use another mail program, e.g. mutt: D to delete all messages matching a pattern, and you can more generally and flexible tag messages according to your needs, and then apply some action to them. – dirkt Jan 17 '17 at 18:24
  • @thrig @dirkt Thanks for the suggestion! I will check out mutt. – nehcsivart Jan 17 '17 at 18:28

I am answering this in case anyone comes by the same question.

It does not appear there is any way to do bulk deletion by pattern matching using mail. An alternative is to use the mutt mail client, which does have such feature:

D \[SPAM\]

Thanks to @thrig and @dirkt for suggesting the alternative.

  • Please accept your answer. – Tripp Kinetics May 21 '18 at 18:30
  • 1
    @TrippKinetics Okay, I have accepted the answer. – nehcsivart May 21 '18 at 18:32
  • Can this be done with mailx for example? I want to do this for my CentOS6 $MAIL and mutt is unhappy with the file format, mailx works fine – Mr Redstoner Jun 13 at 9:32
  • 1
    @MrRedstoner Based on this documentation, you can probably do it with d/[SPAM], but I haven't tested it. – nehcsivart Jun 14 at 18:10
  • @nehcsivart Finally had the chance to try it, it indeed works, thank you. – Mr Redstoner Jun 17 at 18:31

According to "man mail" there are plenty of different patterns to specify the messages on which the command like "d" for deletion will work on. For the asked subject line, you can use

(subject string)
      All messages that contain string in the Subject: field.

In the mail command you would write:

& d (subject Happy Birthday)

This will delete every mail that has the string "Happy Birthday" in its subject. There are also patterns for other fields and different date related criterions (It was tested with "Heirloom Mail version 12.4 7/29/08.")


I was looking for the same without success, so I decided to try like this:

echo 'f 1-$' | mail -N | grep 'SPAM' | awk '{print $1}'

Or print $0 to see the full header to show the mail number with the string 'SPAM'.

So if you think is ok you can delete all of this.

echo 'd #1 #2 #3 ...' | mail -N

Or make a full script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.