7

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\].*$"
nehcsivart
  • 611
  • 7
  • 14
  • 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

3 Answers3

8

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.

nehcsivart
  • 611
  • 7
  • 14
  • 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 '19 at 09:32
  • 1
    @MrRedstoner Based on this documentation, you can probably do it with d/[SPAM], but I haven't tested it. – nehcsivart Jun 14 '19 at 18:10
  • @nehcsivart Finally had the chance to try it, it indeed works, thank you. – Mr Redstoner Jun 17 '19 at 18:31
  • Worked for me! For anyone who might not be very technically minded, note that the \ characters are only present in this code to escape the square brackets. if your subject line is "Test", then you'll only need to do something like mutt -f /home/user-account/mail/.email\@example_com/, then D, then Test. – rinogo Aug 06 '21 at 13:57
3

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.")

1

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.

Christopher
  • 15,911
Carlos
  • 11