Introduction
An administrator should always use dovecot tools to manipulate email messages on the command line to avoid data corruption or other potential issues. The following provides guidance on how to get started with removing email messages via the command line with dovecot tools.
Please keep in mind that the process of manipulating email messages via the command line is an administrative task that is not related to cPanel or the basic configuration of cPanel. cPanel is not able to provide support for the steps provided in the guide below. The following guide is provided as a courtesy. If assistance is required for the steps outlined below, you must reach out to a systems administrator with the skills, training, and expertise required to provide consultation on the issue for you.
Procedure
First, a backup of the account should be taken before any changes are made. You can learn how to do this in the following resource:
***Failure to create a backup beforehand can result in irrecoverable data loss.***
The next step is to learn how to use the doveadm search command along with doveadm search queries.
The two manual pages for these topics can be found below:
Here is an example of a one line script that will print the date a message was saved to the server, along with the subject of the message. Note that you'll need to replace the CPANELEMAIL@DOMAIN.TLD and SEARCH-QUERY portions with your own. These placeholders are in upper case so that they can be easily seen. Please use the appropriate case for your specific situation.
doveadm search -u CPANELEMAIL@DOMAIN.TLD mailbox INBOX SEARCH-QUERY | while read guid uid;do doveadm fetch -u CPANELEMAIL@DOMAIN.TLD "hdr.subject date.saved" mailbox-guid $guid uid $uid;echo "----------";done
Once you are able to use the search command and confirm that your search query matches the messages that you would like to delete, you may then use that search query with the expunge command to delete the messages. The manual page for the expunge command can be found here:
You can then use the expunge command along with the same search query to delete the messages. This guide does not provide an explicit command example because the Dovecot documentation above provides an example of the command.