MailShrink
Safely reclaim disk space on Dovecot Maildir servers.
Releases • Website • Documentation • Security
MailShrink does not simply run
gzipover your Maildir. It understands Dovecot Maildir metadata, preservesINTERNALDATE, validatesS=metadata, uses mailbox locking, detects existing compression, performs atomic replacement, and verifies messages before removing the uncompressed copy.
curl -sL https://github.com/nemke82/mailshrink/releases/latest/download/mailshrink-linux-amd64 \
-o /usr/local/bin/mailshrink && chmod +x /usr/local/bin/mailshrinkARM64:
curl -sL https://github.com/nemke82/mailshrink/releases/latest/download/mailshrink-linux-arm64 \
-o /usr/local/bin/mailshrink && chmod +x /usr/local/bin/mailshrinkWorks on any Linux server — Ubuntu, Debian, AlmaLinux, Rocky, RHEL, or any distro running Dovecot with Maildir storage. Single static binary, no dependencies.
Dovecot with the zlib plugin. Most hosting panels already have it — run mailshrink check to verify.
# Ubuntu / Debian
apt install dovecot-core
# AlmaLinux / Rocky / RHEL
dnf install dovecot1. Verify Dovecot is ready:
mailshrink check2. See how much space you can reclaim:
mailshrink analyze3. Get a detailed plan:
mailshrink plan example.com4. Compress (dry-run first, then apply):
# See what would happen (no files touched):
mailshrink compress --domain example.com --folder Sent --before 2025-01-01
# Actually compress:
mailshrink compress --domain example.com --folder Sent --before 2025-01-01 --applyMailShrink gzip-compresses old email files in place. Dovecot's zlib plugin transparently decompresses them on read — users see no difference.
Safety first:
- Dry-run by default —
compressrequires--applyto modify files - Dovecot check — verifies zlib is enabled before compressing
- Atomic operations — original file untouched until compression succeeds
- Metadata preserved — mtime (IMAP date), ownership, permissions all restored
- Maildir locking — prevents conflicts with Dovecot
- No deletion — MailShrink never deletes email
Measure, don't guess — instead of assuming "gzip saves ~20%", MailShrink samples your actual messages, compresses them in memory, and reports the real measured ratio.
| Command | Description |
|---|---|
check |
Verify Dovecot + zlib plugin readiness |
analyze |
Scan mailboxes, sample messages, estimate savings |
plan |
Detailed per-account/folder breakdown |
compress |
Compress messages (dry-run by default, --apply to execute) |
version |
Print version info |
| Flag | Available On | Description |
|---|---|---|
--path |
all | Base scan path (default: /home) |
--domain |
compress | Target domain |
--account |
compress | Target account (user@domain) |
--folder |
analyze, plan, compress | Target folder (Sent, INBOX, etc.) |
--before |
compress | Only messages before date (YYYY-MM-DD) |
--older-than |
analyze, plan, compress | Age filter (2y, 6m, 90d) |
--apply |
compress | Required to actually modify files |
--json |
all | Machine-readable output |
--provider |
all | Force panel type (cpanel, directadmin, generic) |
MailShrink auto-detects your server layout:
| Panel | Mail Path | Status |
|---|---|---|
| cPanel / WHM | /home/user/mail/domain/account/ |
✅ |
| DirectAdmin | /home/user/imap/domain/account/Maildir/ |
✅ |
| Custom Dovecot | Any Maildir with cur/new/tmp |
✅ |
| Plesk | /var/qmail/mailnames/domain/account/Maildir/ |
🔜 |
For multi-server hosting environments, fleet automation, and scheduled maintenance, see the Production & Bulk Automation Guide:
- 📦 Ansible Deployment — Playbook to deploy and verify MailShrink across your fleet
- 📊 Mass Scanning & JQ — Auditing hundreds of accounts with high concurrency and JSON export
- ⏰ Automated Maintenance Cronjobs — Safe recurring background compression with
ionice&nice - 🚀 Pre-Migration Optimization — Shrink mail storage before server transfers
- 🛡️ Disk Monitoring & Emergency Hooks — Automated reclamation script when disk reaches 90%+
Based on the article How I Saved Gigabytes of Disk Space by Compressing Old Dovecot Maildir Emails on cPanel, which helped reclaim free disk space on production servers. MailShrink turns that manual process into a safe, automated tool.
