How to auto-delete emails from cPanel using a cron job
In most web hosting cases, space occupied by emails are relatively higher than the core-files needed to a website. It is one of the cumbersome tasks for the web hosting provider to control these ratios of hosting space with space for emails. While planning the hosting plans, the hosting provider
However, the real tragedy stands when user aggressively sending or receiving emails in a large. So, the server’s processing resources, as well as disk space usage starts touching the alert level.
So, user or hosting provider will definitely want to get rid off from this email tragedy. Moreover, you can use Gmail for the backup purpose by using mail forwarding system from cPanel. You can use any Gmail address for that, which gives 15GB for emails. So, even you deleted the email from hosting it still having a
Before going to start the tutorial, you should know the path of emails. Generally, cPanel stores emails in home/username/mail directory. You will see some folder like cur, new, tmp and so on. Out of which cur and new folders are having email files in it.
- Generally, new emails arrive in a new folder.
- When the user reads, it gets transferred to the cur folder.
Basically, we are going to focus on these two folders. Because they are having the large chunk of email files.
Following is an SSH command’s files that run it’s own. It can help in deleting email by user time, read/unread status, etc. You must assure that whether your hosting provider supports SSH or not. If SSh is enabled, then you can use the following file-based method. In case, your hosting provider does not support SSH then better to put each command as a separate Cron job.
Mass email deleting using Cron job from cPanel
- Go to cPanel > File Explorer
- Open public_html folder
- Create Folder with any name. This folder will be helpful to collect all cron job in single space. Make sure the folder has at least 755 permission level. So, it is an optional step. You can create the
followingfile directly public_html or anywhere you like.
- Create file with any name and add extension .sh (i.e.
deletemail.sh). Now change the file permission level to 744.
- Put following commands in a new created sh file.
- We have written the command to delete email (both new & read) older than 30 days.
- If you want to delete 8 days old email then kindly replace 30 with 8.
- Please note that this process will delete emails permanently, so before implementing, be assured of backing it up.
find /home/username/mail/websitename.com/accounts/new -mtime +30 -type f -ls -delete
find /home/username/mail/websitename.com/accounts/cur -mtime +30 -type f -ls -delete
- Now come to cPanel > Cron Jobs
- Create new crone job with the following command and put it on daily, weekly, monthly basis or any period you want.
Now, you can observe the email in this account gets deleted automatically. Thanks to Cron Jobs utility. Even if you have any doubt or suggestion then let me know using the comments section. All the best.