Plesk

How to delete all or some emails from a mailbox without logging in to the mailbox on a Plesk server?

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:

  1. Delete the mailbox from the server and create it again - all emails will be removed;

  2. 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 mailbox user@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 mailbox user@example.com:

      # doveadm expunge -u user@example.com mailbox INBOX.'*' before 1w

    • Remove all messages from 'Drafts' folder on all example.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; done

      Note: 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://github.com/plesk/kb-scripts/raw/master/mailbox-doveadm-expunge/mailbox-doveadm-expunge.sh && chmod +x ./mailbox-doveadm-expunge.sh

      • Edit/Create file mbox.txt and specify all mailboxes for you wish to remove emails from:

        johndoe@example.com
        ...
        jjohndoe2@example.org

      • Execute the script:

        # ./mailbox-doveadm-expunge.sh

More information on these features could be found in official Dovecot documentation articles:

Doveadm-Search-Query

Doveadm-Expunge