Removing messages from outbound queue

From CobaltFAQs

Jump to: navigation, search

I originally posted this to the cobaltfacts mailing list 29-May-2005


I had to do some selective mail message purging from an outbound mail
spool (all messages containing a certain email address) -- it was a
bad destination address and there were tens of thousands of bounces
being processed per day for this user (subscribed to multiple lists on
a busy Mailman server).

Anyway, I came up with this one-liner to handle the file removal, and
thought that someone else might find it useful someday. And yes, the
pending files were in the mqueue directory, not q1, q2, etc. This
particular server is running Mailscanner, so individual queues are not
used. The same command will work for "regular" servers, just cd to the
appropriate q? dir first.

  $ cd /var/spool/mqueue
  $ grep -H address@example.com * | awk '{split($0,a,":");print a[1]}' | uniq | xargs rm

Explanation:

  grep -H     prepends the filename to each matching line

  Example:
  $ grep -H user@example.com *
  dfj4QE5Vq32375:From: "Joe Q. User" <user@example.com>
  dfj4QJcbq17162:From: "Joe Q. User" <user@example.com>
  dfj4QJcbq17162:> MSN:  user@example.com
  dfj4QJSiq16373:From: "Joe Q. User" <user@example.com>
  dfj4R239d02778:From: "Joe Q. User" <user@example.com>
  dfj4R27vd03088:> From: "Joe Q. User" <user@example.com>
  dfj4R2IBd03597:> From: "Joe Q. User" <user@example.com>
  dfj4R5mxd13361:> From: "Joe Q. User" <user@example.com>
  dfj4R5mxd13361:>> MSN:  user@example.com
  dfj4RFXpd08759:>> From: "Joe Q. User" <user@example.com>
  dfj4S3tNd14737:> >> From: "Joe Q. User" <user@example.com>


  awk '{split($0,a,":"); print a[1]}'    extracts the filename from each line

  $ grep -H user@example.com * | awk '{split($0,a,":");print a[1]}'
  dfj4QE5Vq32375
  dfj4QJcbq17162
  dfj4QJcbq17162
  dfj4QJSiq16373
  dfj4R239d02778
  dfj4R27vd03088
  dfj4R2IBd03597
  dfj4R5mxd13361
  dfj4R5mxd13361
  dfj4RFXpd08759
  dfj4S3tNd14737


  uniq        makes sure that each file is only listed once (2 dupes in my example,
              msg dfj4QJcbq17162 and dfj4R5mxd13361)

  $ grep -H user@example.com * | awk '{split($0,a,":");print a[1]}' | uniq
  dfj4QE5Vq32375
  dfj4QJcbq17162
  dfj4QJSiq16373
  dfj4R239d02778
  dfj4R27vd03088
  dfj4R2IBd03597
  dfj4R5mxd13361
  dfj4RFXpd08759
  dfj4S3tNd14737


  xargs rm    removes each filename that is passed in from the
              preceding commands
Personal tools