Managing email storage is a crucial part of maintaining efficient mail servers, especially for administrators using Dovecot. Over time, mailboxes can accumulate a massive number of emails, leading to performance issues and potential storage costs. One effective way to manage this is by automatically deleting emails older than a specific period. In this blog, we’ll discuss how to use doveadm expunge to delete old emails.
Understanding the Basics
Dovecot’s doveadm expunge command is a powerful utility for deleting emails based on specified criteria. Here’s a quick overview of the command syntax:
doveadm expunge -u
-u: Specifies the user mailbox.
mailbox '
Use Cases
1. List Existing Mailboxes
Before deleting emails, identify the folders within a specific mailbox. Use the following command:
doveadm mailbox list -u user@example.com
Sample output:
INBOX
INBOX.Spam
INBOX.Drafts
INBOX.Trash
INBOX.Sent
2. Delete Emails Older Than 2 Weeks in All Folders
To remove all emails older than two weeks in all folders for a specific mailbox:
doveadm expunge -u user@example.com mailbox '*' before 2w
3. Exclude INBOX Folder While Deleting
If you want to delete old emails from all folders except INBOX, use:
doveadm expunge -u user@example.com mailbox INBOX.'*' before 2w
4. Delete All Emails in a Mailbox
To delete all emails from all folders within a specific mailbox:
doveadm expunge -u user@example.com mailbox '*' all
Bulk Removal of Old Emails
When managing multiple accounts, you may need to automate the process for all mailboxes on a server. Here’s how to approach this on Plesk and cPanel.
Step 1: Generate a List of Mailboxes
For Plesk:
Run the following command to get a list of all active mailboxes:
plesk db -Ne "select concat(m.mail_name,'@',d.name) as mailbox, m.postbox from domains d, mail m, accounts a where m.dom_id=d.id and m.account_id=a.id and m.postbox='true'" | awk '{print $1}' >mbox.txt
For cPanel:
Generate a list of all mailboxes with:
for i in $(awk '{print $2}' /etc/trueuserdomains); do uapi --user=$i Email list_pops | egrep "\s+email:" ; done | awk '{print $2}' >mbox.txt
Step 2: Automate Deletion with a Script
Create a shell script (mailbox-doveadm-expunge.sh) to process the mailboxes:
#!/bin/bash # Script to delete emails older than 2 weeks from all mailboxes MAILBOX_FILE="mbox.txt" if [ ! -f "$MAILBOX_FILE" ]; then echo "Mailbox list file $MAILBOX_FILE not found!" exit 1 fi for mailbox in $(cat $MAILBOX_FILE); do echo "Processing mailbox: $mailbox" doveadm expunge -u $mailbox mailbox 'INBOX' before 2w doveadm expunge -u $mailbox mailbox 'INBOX.*' before 2w doveadm expunge -u $mailbox mailbox 'Sent' before 2w doveadm expunge -u $mailbox mailbox 'Trash' before 2w doveadm expunge -u $mailbox mailbox 'Drafts' before 2w doveadm expunge -u $mailbox mailbox 'Spam' before 2w done
Save the script and ensure it has executable permissions:
chmod +x mailbox-doveadm-expunge.sh
Run the script:
./mailbox-doveadm-expunge.sh
Best Practices
1. Backup Emails: Before performing a mass deletion, create a backup of your mail directories.
2. Test on a Single Mailbox: Verify your deletion criteria by testing on a single mailbox before applying changes in bulk.
3. Monitor Logs: After running doveadm expunge, check Dovecot logs for errors or warnings.
Conclusion
Using doveadm expunge simplifies email management and helps prevent mail server overload by automatically removing old emails. Whether you’re working with individual accounts or hundreds of mailboxes, this approach can save significant time and effort. Integrate this cleanup process into your routine server maintenance to keep your mail system optimized.