How to Delete Queued Emails by Subject Pattern in Mail Server (Postfix/Qmail/Sendmail)


2 views

When your mail server's queue gets flooded with multiple emails sharing identical subject lines (often from spam or misconfigured systems), it can cause delivery delays and resource issues. The challenge is efficiently removing these queued messages before they're dispatched.

Here are approaches for different mail transfer agents:

For Postfix Systems

The most reliable method uses postsuper command combined with grep:

postsuper -d mailq | grep -B1 'Subject: Your spam subject' | grep ^[A-F0-9] | cut -d " " -f 1

Breakdown:
- mailq lists queued messages
- grep filters for your target subject
- cut extracts just the queue IDs
- postsuper deletes them

For Qmail Systems

Use qmail-queue with some shell magic:

qmail-qread | grep -B1 'Subject: Spam Alert' | grep '^[0-9a-f]' | xargs -n1 qmail-rm

For more complex subject patterns, consider regex matching:

# Postfix example with regex
postsuper -d mailq | awk '/Subject: $$URGENT$$.*SPAM/ {print $1}'

Always verify before mass deletion:
1. First preview affected emails:

mailq | grep -A10 'Subject: Target Subject'

2. Consider backing up the queue directory
3. Test with a single deletion first

To prevent recurrence, implement content filtering in your MTA configuration. For Postfix:

# In main.cf
header_checks = regexp:/etc/postfix/header_checks

# In /etc/postfix/header_checks
/^Subject:.*spam phrase/i REJECT Spam detected

When your mail server's queue gets flooded with hundreds of identical spam messages, you need surgical precision to remove them without affecting legitimate emails. Here's how to tackle this in different MTA environments.

For Postfix systems, use the postsuper command with message filtering:

# First list matching emails (dry run)
postqueue -p | grep -B 1 "Subject: Your spam subject" | grep ^[A-F0-9] | awk '{ print $1 }'

# Then delete them
postqueue -p | grep -B 1 "Subject: Your spam subject" | grep ^[A-F0-9] | awk '{ print $1 }' | xargs -n 1 postsuper -d

With Qmail, we need to work with the maildir structure:

# Find messages with specific subject
find /var/qmail/queue/mess -type f -exec grep -l "Subject: Spam Alert" {} \; | \
while read file; do
  qmqtool -d ${file##*/}
done

For Sendmail systems, use the mailq output parsing:

mailq | grep "Subject: Unwanted promotion" | awk '{print $1}' | sed 's/\*//g' | xargs -n 1 sendmail -qG

For more complex subject line patterns, consider Perl one-liners:

mailq | perl -ne '/^\h*(\w+).*\n\h*Subject:\h*(.*spam.*)/i && print "$1\n"' | xargs -n 1 postsuper -d

To avoid future occurrences, implement these measures:

# Postfix header_checks
/^Subject:.*viagra.*/ REJECT

# Or in main.cf:
header_checks = regexp:/etc/postfix/header_checks

When processing large queues (>10K messages), use more efficient methods:

# Use find instead of grep for huge queues
find /var/spool/postfix/deferred -type f -exec grep -l "Subject pattern" {} + | \
xargs -P 4 -I {} sh -c 'postsuper -d $(basename {})'