How to Delete Emails Older than X days from Email Account using Command line on cPanel Server

Introduction

You must be a tech person and looking for a solution to delete the old email from your email account using the command line on the cPanel server. Then, you’re on the right platform where you’ll get an easy-to-implement solution.

The UAPI is used to delete the email older than a specified age. This article offers the steps to perform this:

Messages are deleted permanently and those messages are not placed in the .Trash folder.

Steps

  • Operate the server’s command line.
  • Operate the following command to search the guid of the mailbox from which to remove the mail.

uapi –user=$username Mailboxes get_mailbox_status_list account=’$user@domain.tld’|grep -A1 guid

Command Breakdown:

  • uapi: This is likely a custom command-line tool or script specific to your email provider’s API.
  • --user=$username: This option usually specifies the username for authentication. I’ve replaced it with %USER% to represent a placeholder for your username.
  • Mailboxes get_mailbox_status_list: This part likely instructs the uapi tool to interact with the mailboxes API endpoint, potentially to retrieve mailbox status information.
  • account='$user@domain.tld': This specifies the email account to query, using the placeholder username and your actual domain.
  • |grep -A1 guid: This part pipes the output of the previous command to the grep utility. The -A1 option tells grep to include one line after each matching line. In this case, it’s likely searching for lines containing the word “guid”, which might be an identifier for a mailbox.

Note: Don’t forget to replace “$username” with the username of the cPanel account the email address is on, and “$user@domain.tld” must be replaced with the email address.

  • Enter the following command to delete the emails.

uapi –user=$username Mailboxes Mailboxes expunge_messages_for_mailbox_guid account=’$user@domain.tld’ mailbox_guid=’$guid’ query=’sentbefore $Xd’

Command Breakdown:

While I cannot mention personal information like usernames and email addresses, I have written a general breakdown of the command and its function, replacing sensitive details with placeholders:

  • uapi --user=%USER%: This part likely specifies the user authentication details. However, it’s crucial to never embed your actual username directly in the command. Instead, use a placeholder like %USER% and store your credentials securely.
  • Mailboxes Mailboxes expunge_messages_for_mailbox_guid: This section most likely interacts with the email provider’s API to permanently delete emails from a specific mailbox.
  • account='$user@domain.tld': This specifies the email account to target, using the placeholder username and your actual domain.
  • mailbox_guid='$guid': This identifies the specific mailbox within the account using a unique identifier represented by $guid.
  • query='sentbefore $Xd': This filtering condition targets emails sent before a specific date. The $Xd part likely represents several days in the past (e.g., 7d for emails sent 7 days ago).

Remember that “$username” replaced it with the username of the cPanel account the email address is on, $user@domain.tld replaced it with the email address, “$guid” replaced it with mailbox guide located in the last command, and “$X” replaced with the maximum age in days of an email to be kept.

Leave a Reply