mailcow-dockerized-docs/docs/u_e-dovecot-expunge.md
Tobias Strobel c1cb6c9e07
Fix cronjob errors of Dovecot expunge
Recently, I discovered that the cronjob on my mailcow-dockerized host isn't working.
If the script is called via crontab, the docker-compose command doesn't know where the config file is located. So I added the "cd" to change the directory to the working directory of the mailcow-dockerized instance.
When executing the docker-compose exec command via cronjob, it fails with error message "the input device is not a TTY". By adding the parameter "-T" it works.
2018-04-08 14:33:14 +02:00

1,8 KiB

If you want to delete old mails out of the .Junk or .Trash folders or maybe delete all read mails that are older than a certain amount of time you may use dovecot's tool doveadm man doveadm-expunge.

The manual way

That said, let's dive in:

Delete a user's mails inside the junk folder that are read and older than 4 hours

docker-compose exec dovecot-mailcow doveadm expunge -u 'mailbox@example.com' mailbox 'Junk' SEEN not SINCE 4h

Delete all user's mails in the junk folder that are older than 7 days

docker-compose exec dovecot-mailcow doveadm expunge -A mailbox 'Junk' savedbefore 7d

Delete mails inside a custom folder inside a user's inbox that are not flagged and older than 2 weeks

docker-compose exec dovecot-mailcow doveadm expunge -u 'mailbox@example.com' mailbox 'INBOX/custom-folder' not FLAGGED not SINCE 2w

!!! info For possible time spans or search keys have a look at man doveadm-search-query

Make it automatic

If you want to automate such a task you can create a cron job on your host that calls a script like the one below:

#!/bin/bash
cd /opt/mailcow-dockerized
/usr/local/bin/docker-compose exec -T dovecot-mailcow doveadm expunge -A mailbox 'Junk' savedbefore 2w
/usr/local/bin/docker-compose exec -T dovecot-mailcow doveadm expunge -A mailbox 'Junk' SEEN not SINCE 12h
[...]

To create a cron job you may execute crontab -e and insert something like the following to execute a script:

# Execute everyday at 04:00 A.M.
0 4 * * * /path/to/your/expunge_mailboxes.sh