blog posts

How to Automatic Clear Cache in WordPress through Cron job

Have you ever noticed that the page loading speed of a website slows down after clearing your browser history? Usually, every time you clear your browser’s memory, the speed and loading time of the websites you visited will return to the original state.

The cache is actually a copy of website information that is collected after the user’s first visit to the site and presented to him as a website page. This information needs to be deleted and reloaded after some time. Cron Job is a software that can delete this information automatically. In this article, we explain how to automatically clear the cache memory in WordPress through cron job.

So what is the reason for this? In other words, when you visit a website for the first time, you receive the information directly from the web server to which the website is connected. At the same time, both the website server and your browser make a copy of the website page information for future use and store it in the cache memory.

Therefore, in the future, to visit the website, you no longer need to connect directly and receive information from the web server itself, and with a faster connection to the cache or temporary memory, you can access the desired website. However, it is necessary for website owners to periodically clear the cache in WordPress.

In general, there are many ways to clear the cache memory and increase the speed of the website. You can increase the loading speed of your website pages by using plugins such as LiteSpeed Cache. In this article, we introduce how to automatically clear the cache memory in WordPress through cron job.

 

Table of Contents

  • Why do we clear the cache memory?
  • What is cron job?
  • Automating the clearing of cache memory in WordPress through cron job
  • Conclusion

Why do we Clear the Cache Memory?

If the cache memory is effective in increasing the speed and reducing the loading time of the website, why should we delete it? As mentioned earlier, the cache memory is formed both in the web server and in the user’s browser. Every user clears the cache according to his needs or interest (which is generally to clear his search traces on the Internet).

But the cache or temporary memory that is prepared on the web server or the WordPress part of the website can cause the website to slow down over time. Therefore, one of the reasons for periodically clearing the cache memory is to prevent excessive storage of files in this memory.

Also, after the cache memory is formed, since the user does not connect to the main web server every time he visits the site, he will not be informed about the changes made by the website in the content, skin and other areas, because he sees the previous cache version.

For this reason, it is necessary to clear and update the WordPress cache of the website so that users can see the latest website changes.

Finally, another reason that is related to the speed of website page loading and is important for many business owners is the relationship between the SEO score and the rise of the website, so that users on the Internet quickly leave websites with a load time of more than 3.5 seconds.

Google is sensitive to this factor and shows websites with lower load times in higher ranks on its search pages.

For the above reasons and some other minor reasons such as having a better user interface or UI, it is necessary to clear the cache memory in WordPress. But for websites with high traffic, this can be a problem. Because clearing the cache memory during peak visits and generally during the day does not seem like an interesting option.

That is why there is a need for tools to do this automatically at any time of the day or night.

 

What is Cron job?

Cron Job is a software that helps automate repetitive processes. Through the cron job software, you can do many website tasks automatically and at specific times without allocating time, energy, capital and resources, one of which is the automatic clearing of the cache memory in WordPress.

This software exists by default in various control panels such as cPanel or Direct Admin and is used in highly visited websites in cases such as automatic cache memory deletion. Also, note that this software is only a doer and we must provide codes to it to execute them.

Automating the clearing of cache memory in WordPress through cron job

We have two ways to add a cron job; One is to run it on the main server and the other way is to use the cron job available on WordPress, which we will teach both of these ways.

But in the beginning, like the rest of the training, we need to have some prerequisites. First, install the WP Rocket plugin on WordPress and activate the Cache preloading option in the Preload options section of this plugin. Also, disable the automatic cache deletion option.

Now, after setting the prerequisites, we need to create a cron job to clear and reload the WordPress cache at the specified time.

Step 1: Create a PHP file to clear the WordPress cache

First, we need a PHP file containing commands to clear the cache of HTML and CSS/JS files. You can create this file through any editor software. Also, if you have access to CPanel or another control panel, you can also do this through the file manager. Create a file named rocket-clear-cache-cron.php and enter the following code in it:

<?php 
// Load WordPress.
require( 'wp-load.php' );
// Clear cache
// Also preload the cache if the Preload is enabled.
if ( function_exists( 'rocket_clean_domain' ) ) {
   rocket_clean_domain();
 }
// Clear minified CSS and JavaScript files.
if ( function_exists( 'rocket_clean_minify' ) ) {
   rocket_clean_minify();
} 

 

Now save this file and upload it to the root folder with the name “public_html” or “www” which varies based on the type of server. If you are working through cpanel you can save it in public_html

Note: If you plan to save this file somewhere else, be sure to specify its location in the “wp-load.php” section in the third line of the above code.

Step 2: Create a cron job

Now that the PHP file is ready, we need a cron job to run it. Also, as we mentioned before, we have two ways to create a cron job; Action through cron job on the server or using WordPress cron job. These methods are as follows:
The first method: installing cron job on the server

This method is our first priority in automating the process of deleting the cache memory in WordPress with cron job because it is executed directly on the web server and at the set time.

To do this, open the wp-options.php file located in the root folder where your WordPress website is installed. In the final lines of the codes, the line ‘That’s all, stop editing! Find ‘Happy publishing’ and enter the following code on the top line:

 

define('DISABLE_WP_CRON', true);

 

Now the WordPress cron job is disabled and it’s time to create a cron job to run on the server. Enter the control panel of your website and the Cron Jobs section.

Cron job

 

Entering the cron job section in cPanel
Set notification email

If you want to receive notifications of cron jobs, enter your email address in the Cron Email field and click on the Update Email option.

Cron job

 

 

Otherwise, to disable all cron job notifications, just delete the email address. But if you want to disable certain cron job notification, you have to do it in another way.

Find the desired cron job in the Current Cron Jobs table and click the Edit button. In the Command line section, at the end of the existing code, enter the following statement and save the file:

/dev/null 2>&1


Cron job

/usr/local/cpanel/bin/is_script_stuck /dev/null 2>&1

 

Setting cron job scheduler

Now you can set its execution cycle in the cron job schedule menu. For this purpose, you can choose the appropriate options from the available lists or enter the selected values in the empty spaces.

In the Common Settings section, you can choose the general template; For example, running cron jobs on a weekly, monthly or daily basis.

 

  • In the Minute section, you must enter a number between 0 and 59 that refers to the minute.
  • In the Hour section, you can enter a number between 0 and 23 that refers to the running hour.
  • In the Day section, you must enter a number between 1 and 31 that refers to the day.
  • In the Month section, you can enter a number between 1 and 12 that refers to the month.
  • In the Weekday section, you must enter a number between 0 and 7 that refers to the day of the week.

 

 

Enter the cron job command

At the beginning of the article, we mentioned that the cron job is just a doer of work and we need to define a task for it to do it at the specified time. In this step, we need to give a command to the cron job we created to run it at the time we specified. Enter the following command in the Command field:

wget -q -O - https://yourdomain.com/wp-cron.php?doing_wp_cron &gt;/dev/null 2&gt;&amp;1

 

Be sure to replace the main address of your website with yourdomain.com in the code above. At the end, click on the Add New Cron Job option to create a new cron job.

But this is not the end. We need another cron job to execute the PHP file that we have made, to clear the cache memory and preload it (preloading: loading the cache page before receiving a request from the browser)
Creating a secondary cron job

Create a new cron job by going through the steps above and entering the following command:

 

Second method: Setting cron job in WordPress to clear the cache memory

If you prefer to stay away from server complications, you can use WP Crontrol plugin to create cron jobs. Install this plugin and go to WP Admin > Tools > Cron Events > Add Cron Event.

 

 

Conclusion

At a time when the average duration of videos on social networks has reached below 10 seconds, the cache section of any website is one of the most important parts in terms of improving the speed of the site and finally attracting the audience. The managers of big websites pay great attention to this section of their website so that they can provide the user with the latest version of each page of their website in the shortest time and with the fastest speed.

By following the described procedure, you can automate the process of clearing the cache memory in WordPress through cron job. Also, after learning these methods, you should keep some points in mind. First of all, you must consult your website’s virtual server support unit about this before any action, and the second point is that clearing the cache memory frequently and in short intervals can damage the server. So keep this point in mind while setting the cron job time cycle.