Automatically clean up ProtonMail folders based on retention policies.
- Docker and Docker Compose installed
- ProtonMail Bridge running on host machine
-
Copy the example config:
cp config.json.example config.json
-
Edit
config.jsonwith your settings:- Update email and password (use Bridge password)
- Configure folders and retention days
- Set
dry_run: truefor testing (or per-folderdry_runoverrides)
-
Build and start:
docker-compose up -d
View logs:
docker-compose logs -fRun manually:
docker-compose run --rm protonmail-cleanup python cleanup.pyStop service:
docker-compose downEdit config.json:
imap.server: Usehost.docker.internalfor Bridge on hostimap.port: Default is 1143 for Bridgeimap.starttls: Set totrueif your Bridge requires STARTTLS (some Linux setups)imap.starttls_insecure_skip_verify: Set totrueto allow self-signed certs (INSECURE; prefer installing the CA)imap.username: Optional; if omitted,emailis used for IMAP logindry_run: Set totrueto test without deleting (per-folder override available)log_to_file: Set tofalseto disable writing to/app/logs/cleanup.loglog_mailboxes: Set totrueto log the list of available mailboxes (helps debug folder names)folder_prefix: Optional prefix applied to custom folders (e.g.,Folders/if your custom folders are nested). It is not applied to common system folders (Spam, Trash, Starred, Sent, Drafts).imap_timeout_seconds: Optional socket timeout for IMAP operations (default 60s).imap_connect_retries: Optional number of connection/login retries (default 5)imap_connect_retry_delay_seconds: Optional delay between retries (default 10s)sender_rules: Optional global sender-based retention overrides. Each rule:from_contains: substring match against the From header (case-insensitive)subject_contains: substring match against the Subject (case-insensitive)from_regex: regex match against the From header (case-insensitive)subject_regex: regex match against the Subject (case-insensitive)retention_days: positive integer
folders: Array of folders with retention policies; each entry supports:name: Folder name (exact match)retention_days: Positive integer, ornullfor unlimited (only sender/subject rules will apply)dry_run(optional): Override global dry_run for this foldersender_rules(optional): Same shape as global sender_rules; evaluated before global rules
Notes:
- The script reads the From header in a case-insensitive manner for sender rules.
- Adjust the retention days and folders to suit your preferences. Using small retention values can result in unintended data loss.
- Ensure ProtonMail Bridge is running on host
- Check Bridge allows connections from Docker network
- Verify folder names match your ProtonMail folders exactly
- Logs are written to the container console and
/app/logs/cleanup.log(mapped to./logslocally). - Set
log_to_file: falseinconfig.jsonif your runtime filesystem is read-only. - Validation errors (missing config keys, invalid retention values) are logged and stop the run.
Install dev deps:
pip install -r requirements-dev.txtRun tests:
python -m pytest- Create
config.jsonalongside the script; it will be used automatically if/app/config.jsonis missing. - Or set
CONFIG_FILE=./config.json(or another path) in your environment/IDE run configuration.