Skip to content
Alfa Intelli
  • Home
  • About
  • Services
  • Knowledge Base
  • Tools
  • Contact Us
  • Alfa Intelli Tech

Automatically Clean the Junk Folder and Trash Folder

20 February 2023 · Email

← Knowledge Base

Automatically Clean the Junk Folder and Trash Folder

To delete emails in Junk folder for all users, you can run

sudo doveadm expunge -A mailbox Junk all

To delete emails in Trash folder, run

sudo doveadm expunge -A mailbox Trash all

I think it’s better to clean emails that have been in the Junk or Trash folder for more than 2 weeks, instead of cleaning all emails.

sudo doveadm expunge -A mailbox Junk savedbefore 2w

Then add a cron job to automate the job.

sudo crontab -e

Add the following line to clean Junk and Trash folder every day.

@daily doveadm expunge -A mailbox Junk savedbefore 2w;doveadm expunge -A mailbox Trash savedbefore 2w

You also need to allow the www-data user to read certificate files by adding the following line.

@daily setfacl -R -m u:www-data:rx /etc/letsencrypt/live/ /etc/letsencrypt/archive/

To receive report when a Cron job produces an error, you can add the following line above all Cron jobs.

MAILTO="you@your-domain.com"

Save and close the file. And you’re done.

Tags: email, ubuntu

← Dovecot Automatic RestartAuto-Renew TLS Certificate →

Related guides

Block Email Spam with Postfix

2 March 2023Read the guide

Increase Upload File Size Limit with Ubuntu/PHP

28 February 2023Read the guide

Install Roundcube Webmail with Nginx

28 February 2023Read the guide

Need help with your IT?

Let's talk

Contact Us

India

74-81 District Shopping Centre
Sri Ganganagar
Rajasthan - 335001

Australia

241-243 Princes Highway
Corrimal
NSW - 2518

Trading Hours

  • Monday - Friday --- 10 AM to 6 PM (IST)
  • Saturday -- Close
  • Sunday -- Close

© 2026 Alfaintelli Group