Delete old messages from one or more IMAP accounts based on per-folder criteria:
- days:N — delete messages older than N days
- last:N — keep only the last N messages
Starred (flagged) messages are never deleted.
- PHP >= 7.4
- PHP imap extension
Note: The
ext-imapextension is deprecated as of PHP 8.4 and will be removed in a future PHP version. A migration path will be evaluated when that happens.
Copy the included imap_maint_config_example.php to imap_maint_config.php
and add your IMAP account details. You can configure as many accounts and
folders as you like.
Keep your credentials secure:
chmod 0600 imap_maint_config.phpFor details on the mailbox parameter format, see the
PHP imap_open documentation.
# Normal run (e.g. from cron)
./imap_maint.php
# Preview what would be deleted, without deleting anything
./imap_maint.php --dry-run
# Show per-message details during deletion
./imap_maint.php --verbose
# Show help
./imap_maint.php --helpRun daily as a cron job. The script connects to IMAP servers remotely, so any host with PHP and shell access will work:
30 7 * * * /path/to/imap_maint.phpOutput is timestamped for log readability. The exit code is 0 on success, 1 if any account or folder had errors.
Released under the MIT License. See the bundled LICENSE file for details.