Applicable to:
- Plesk for Linux
Question
How to delete all or some emails from a mailbox without logging in to the mailbox?
Answer
Right now this feature is not implemented in Plesk. Please vote for it on our Plesk UserVoice portal: the most popular features are likely to be included in one of the next Plesk versions.
As a workaround, one of the following methods can be applied:
-
Delete the mailbox from the server and create it again - all emails will be removed;
-
Directly manipulate with Dovecot administrative utilities on a server:
Note: SSH access to the server is required.
Mailbox should be enabled: The "Mailbox" option is checked in Domains > example.com > Email Addresses > joedoe@example.com-
Get the information about existing mailboxes:
# doveadm mailbox list -u user@example.com
INBOX
INBOX.Spam
INBOX.Drafts
INBOX.Trash
INBOX.Sent -
Remove all messages older than a week from the
'Inbox'
folder for the particular mailboxuser@example.com
:# doveadm expunge -u user@example.com mailbox '*' before 1w
-
Remove all messages older than a week from all folders except
'Inbox'
folder for the particular mailboxuser@example.com
:# doveadm expunge -u user@example.com mailbox INBOX.'*' before 1w
-
Remove all messages from
'Drafts'
folder on allexample.com
domain mailboxes:# for i in $(plesk bin mail -l | tr '\t' ' ' | cut -d' ' -f 3- | grep example.com); do doveadm expunge -u "$i" mailbox INBOX.Drafts all; done
-
Delete all messages from the particular mailbox
user@example.com
:# doveadm expunge -u user@example.com mailbox 'INBOX' all
# doveadm expunge -u user@example.com mailbox 'INBOX.*' all -
Remove all messages from
'Spam'
and'Trash'
folders from all mailboxes on all domains:# for i in $(plesk bin mail -l | tr '\t' ' ' | cut -d' ' -f 3-); do doveadm expunge -u "$i" mailbox INBOX.Spam all; done
# for i in $(plesk bin mail -l | tr '\t' ' ' | cut -d' ' -f 3-); do doveadm expunge -u "$i" mailbox INBOX.Trash all; doneNote: courier-imap does not support these commands
-
Also the procedure can be optimized by deleting all emails for all mailboxes specified in a file:
-
Download the following script and grant permissions:
# wget https://support.plesk.com/hc/en-us/article_attachments/360009079459/script.sh
# chmod +x ./script.sh
-
Edit/Create file mbox.txt and specify all mailboxes for you wish to remove emails from:
CONFIG_TEXT: johndoe@example.com
...
jjohndoe2@example.org -
Execute the script:
# ./script.sh
-
-
More information on these features could be found in official Dovecot documentation articles:
Comments
4 comments
Hi,
I'm looking at running a daily job to delete all spam items older than 1 month (like Gmail does).
Can I confirm this command will do the trick?
Thanks.
Nathan Wilson syntax looks correct, thought we do not test any anything like this to guarantee it will work for 100%.
p.s. I would do a small change to your command to exclude aliases as they have no separate mailbox:
That is a great one! thanks for that.
This script solves a major problem with the clients that using gmail pop3 to get their emails.
Gmail does not grab the spams and the plesk mailboxes getting full.
Anton Maslov & Nathan Wilson, the `1m` in your snippets would remove all mails older than 1 minute.
From dovecot's documentation, supported time units are:
I've just made that mistake, thankfully it was only on my emails in the trash folders.
I'd suggest you use 31d to cover the longest months.
Please sign in to leave a comment.