Skip to content

Menu
  • Home
  • Linux Hosting
  • WordPress
  • PHP
  • cPanel
  • Digital Marketing
  • Linux Reseller Hosting
  • VPS
  • Dedicated Server
  • Tutorials
  • Website Development
  • Client Area
March 11, 2022
HomecPanelCron JobsHow To Automatically Delete Older Webmail Emails Via Cron Jobs

How To Automatically Delete Older Webmail Emails Via Cron Jobs

By Faisal K cPanel, Cron Jobs, Webmails  0 Comments

Maybe you’ve signed up for a newsletter or signed up somewhere long ago and from then, they’re continuously sending you emails regularly without giving any unsubscribe option. Or maybe someone just keeps sending you offers. Or someone is just randomly sending you promotional emails daily, and you’re fed of those emails. Or, you just want all your emails to be auto-deleted over a selected time as the emails you’re getting aren’t important enough to always keep in your mailbox.

how to auto- delete older emails from webmails

 

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

  1. A custom Bash Script.
  2. Bash (.sh) file inside the root directory of cPanel.
  3. 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.

cPanel Advanced Settings

 

  •  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.

Cron Jobs Common Settings

  • 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

Root directory public_html

  • Create a new .sh file with any name (example.sh).

Create new sh file

  • 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.

PHP Script to automatically delete emails

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.

Cron jobs command

  • 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. 

Add a new cron job

  • You can view your newly created cron job inside the Current Cron Jobs section, and also you can modify them from the Actions tab.

Current Cron Jobs section

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.

Share this post with your friends and increase their awareness.

For any further assistance, you can reach out to us on our Facebook or Twitter account.

Tags:auto-delete, automatically, bulk emails, cpanel account, cron jobs, delete, delete emails, delete older emails, emails, facebook, howto, mail, public_html, root, twitter, webmails

Related Posts

How to setup custom static homepage in WordPress site

How to setup custom static homepage in WordPress site?

What is bandwidth in web hosting| explained

What is the meaning of Bandwidth in cPanel web hosting?

defaultnameserver-redserverhost

How to set default nameservers in WHM?

Search

Our Recent Posts

  • Website Down! Are you not able to access a particular website on your PC?
  • How to Manage Customer Addresses in AbanteCart?
  • How to Fix “Add New Plugin Menu Not Showing” In WordPress?
  • How to Add YouTube Subscribe button to your WordPress website?
  • How to Disable WordPress Plugins from Database?

Categories

Search

Red Server Host Copyright © 2023.
Powered by Skillvent Technologies Pvt. Ltd. . Back to Top ↑