You can add Automated deletion of emails from your cPanel via cron jobs and a bash script if you want your older emails to be deleted automatically over your selected time.
Here’s How.
Things You Need To Delete Emails Automatically Via Cron Jobs
- A custom Bash Script.
- Bash (.sh) file inside the root directory of cPanel.
- Cron Jobs custom command.
Automatically Delete Older Emails Via Cron Jobs – In Detail
- Log in to your cPanel account.
- Inside the ADVANCED section, click over the Cron Jobs tab.
- Click over the Common Settings drop-down and select the Once Per Day option.
However, you can increase or decrease the time limit of cronjobs if you want. This setting will hit the cron job once a day to check and delete all the emails that are above 30 days or older.
- Now leave this window and open your cPanel account again in a new tab.
- Go inside the File Manager and then inside your Root directory.
Also Read: Protect your Server from Spam Emails
- Create a new .sh file with any name (example.sh).
- Open that file and paste the below script.
#/bin/sh find /home/USERNAME/mail/*/*/cur/* /home/USERNAME/mail/*/*/new/* -type f -mtime +30 | xargs rm -f
- Change the USERNAME with your cPanel username. You can also increase or decrease the time according to you. Currently, the email deletion is set to 30 days.
Note: This method will delete all the (30 days older) emails from all the email accounts associated with your cPanel. If you want to perform this task with a selected email only, this method isn’t for you.
- After pasting the command, change the USERNAME with your cPanel username & hit the Save Changes button.
- Now go back to your first tab where you’ve opened the Cron Jobs section, and find the Command box.
- Paste the following command and hit the Add New Cron Job button.
/home/USERNAME/public_html/example.sh > /home/USERNAME/public_html/ANYNAME.php
- Again, change the USERNAME with your cPanel username & bash file name with your custom file name (If created with a custom name). The output of the cronjob can be viewed inside the ANYONE.php that will be auto-created once a cron job will hit the example.sh file.
The file I want to hit via cron job is present inside my /home/getintod/public_html, if your file path is different than this, you can modify it with yours.
- You can view your newly created cron job inside the Current Cron Jobs section, and also you can modify them from the Actions tab.
That’s it. Now you don’t have to manually delete the older messages. The cron job will check and delete all the 30 days older emails daily.