Remove old messages from Plesk/Qmail Maildir mailboxes

I needed to remove messages older than a week old for a list of email accounts on a Plesk system running Qmail. I put together the following BASH script to locate the messages using find and back them up outside of the mailbox (just in case). This builds on the technique I used in my previous post, Maintain directory structure copying `find` results.

All you need to do is create a file with one address per line to be read in by the command.

cd /var/qmail/mailnames; \
IFS="@"; \ 
while read LINE ; \
do \
    ADDR=( ${LINE} ); \
    echo "${ADDR[0]}@${ADDR[1]}"; \
    find ${ADDR[1]}/${ADDR[0]}/Maildir/ \
        \( -path "*/cur/*" -o -path "*/new/*" \) \
        ! -type d -mtime +7 | \
        while read FILE; \
        do \
            DIR=$( dirname ${FILE} ); \
            mkdir -p /path/to/dst/${DIR}; \
            mv ${FILE} /path/to/dst/${DIR}; \
        done; \
done < /path/to/address_file

Leave a Reply