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

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://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:

    johndoe@example.com

    jjohndoe2@example.org

  • Execute the script:

      • ./script.sh

 

Katen Doe

Katen Doe

Hello, I’m a content writer who is fascinated by content fashion, celebrity and lifestyle. She helps clients bring the right content to the right people.

Press ESC to close