Development and databases
Delete old emails automatically in cPanel
Email is usually what takes up the most space on a hosting account. To stop it growing out of control, you can automatically delete emails older than a number of days, for example those older than 60 days.
You could do this with a rule in Outlook or Thunderbird, but that doesn't help if email is read on a phone or in webmail, and in a company with many accounts it's best not to depend on each user setting up their rules correctly. With a cron job in cPanel you're in control, for as many accounts as you want.
Emails are deleted permanently: they don't go to the Trash. If any account has email that must be kept, save it first (see download email to your computer) or leave that account out.
Test it by hand first
cPanel has a tool to do the same clean-up once: in Email > Email Disk Usage, choose the account, the folder and one of the deletion options, such as messages older than a year. Use it if you only need to free up space as a one-off.
To automate it, we use the same mechanism through the cPanel API, with the uapi command. You need SSH access to test it, or the cPanel terminal.
Delete old emails from one account
This command deletes emails received more than 60 days ago from the inbox of usuario@ejemplo.com:
uapi Mailboxes expunge_mailbox_messages account='usuario@ejemplo.com' mailbox='INBOX' query='savedbefore 60d'
-
account: the email address.
-
mailbox: the folder.
INBOXis the inbox; the others are calledINBOX.Sent(Sent),INBOX.Trash(Trash),INBOX.spam… To see the exact names for an account:uapi Mailboxes get_mailbox_status_list account='usuario@ejemplo.com' -
query: which emails are deleted.
savedbefore 60dare those that reached the server more than 60 days ago. Change the number to the days you want to keep; it also accepts weeks, such assavedbefore 8w.
If the response includes status: 1, it's done. Check in webmail that only the old emails have gone.
If you also want to clean up Sent or the Trash, repeat the command with their folder.
Every account on a domain or on the whole hosting account
Create a script that goes through the accounts. In File Manager or over SSH, create the file /home/usuario_cpanel/purgar-correo.sh (outside public_html) with this content:
#!/bin/bash # Days of email to keep DIAS=60 # Domain to clean up. Leave it empty ("") for every account on the hosting DOMINIO="ejemplo.com" for cuenta in $(uapi Email list_pops | awk '/email:/ {print $2}' | grep "@${DOMINIO}"); do uapi Mailboxes expunge_mailbox_messages account="$cuenta" mailbox='INBOX' query="savedbefore ${DIAS}d" >/dev/null done
Replace usuario_cpanel with your cPanel username, and DIAS and DOMINIO with your values. If an account mustn't be touched, exclude it by adding | grep -v 'jefe@ejemplo.com' after the grep on the for line.
Run it once by hand to check that it does what you expect:
bash /home/usuario_cpanel/purgar-correo.sh
Schedule it every day
Finally, create a cron job in cPanel that runs it once a day, in the early hours:
- Common Settings: Once Per Day (
0 0 * * *). It's better to change the minute and hour to something like17 4 * * *so it doesn't coincide with other jobs. - Command:
/bin/bash /home/usuario_cpanel/purgar-correo.sh >/dev/null 2>&1
From then on, emails older than the age you've set will be deleted every day. The freed-up space shows in cPanel when the disk usage calculation is updated, which can take a few hours.
If it doesn't delete anything
- The account must exist with exactly that name: check it with
uapi Email list_pops. - Check the folder name with
get_mailbox_status_list. - If the email was imported from another server (for example with imapsync), the "saved" date is the date of the import, not of the original email. In that case use
before 60d(date received, which imapsync preserves) orsentbefore 60d(date written in the message itself).
You may also find useful
- Create and run cron jobs in cPanel Set up cron jobs in cPanel, define intervals and commands, manage notifications, call URLs with cURL...
- Persistent processes: queue workers, Horizon and always-on commands Keep Laravel queue workers, Horizon, a persistent Redis or your own command running on the Avanza, M...
- Apache handlers Set from cPanel which Apache handler should process a given extension, and learn why this option cal...
- Available binaries and tools - FFmpeg, Tesseract, ImageMagick, cURL and more See which binaries are available for video, images, PDF, OCR, networking, DNS, FTP and SFTP, version...
Something wrong or out of date? Tell us and we will review it.