WP Rocket – Cron job to clear cache & preload at the scheduled time
This solution illustrates the way of clearing the cache and preloading the content at a specific scheduled time.
It can be useful for large websites with traffic spikes as you can clear cache and preload everything at off-peak hours.
There are two methods of adding the cron job in WordPress. The first is to set up a real cron job that gets executed on a server. The second is to use the WordPress built-in cron job.
Before we continue, please access your WP Rocket plugin settings and enable Cache preloading under WP Rocket > Preload options tab.
Also, please disable automatic cache clearing, see more here: https://docs.wp-rocket.me/article/137-disable-automatic-cache-clearing
Setting up corn job to clear cache at the scheduled time
After everything is prepared, we can start with creating a corn job that will clear and preload cache at the defined time.
We will create a simple PHP file that will contain a small function that will clear the HTML cache and also CSS/JS cached files as well.
You can create the file using your favorite editor or you can create a new PHP file using cPanel file manager, if your website is hosted under this control panel, for example.
You can name your file like rocket-clear-cache-cron.php and paste this code inside:
Now save this file and upload it to your WordPress website root folder, usually it’s “public_html” or “www” depending on the server type. If you are on cPanel, you can save the file inside “public_html”.
NOTE: If you want to upload this file on different folder/directory, make sure to append the path to “wp-load.php” file referenced in the code above, at the 3rd line.
Now that the PHP code is ready, we need to set up a cron job.
Method 1: Setting up a Cron job on the server
This is the method I prefer as it is a real cron job that will be executed at the exact time we define. Unlike the WordPress built-in Cron job, this one doesn’t need any website activity to get executed.
So, let’s open up the file wp-options.php for editing. The file is located inside the root folder of the WordPress website installation.
Scroll down and find this line of code: ‘That’s all, stop editing! Happy publishing.’ and paste this code above that text:
This will disable the WordPress cron.
Now we need to add the server-side cron job that will get executred instead.
Adding a cron job can depends on the server type and access level. If you are using cPanel, please check this quick video on how to add the cron: https://www.youtube.com/watch?v=t5mjWGegE-g
The code that we need to add it this:
wget -q -O - https://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
Now, make sure to replace yourdomain.com with your real website URL.
Set the cron job to execute every 5 minutes. To do so, just enter the values in this format:
*/5 * * * *
We also need to add another cron job that will execute our PHP file for clearing & preloading cache.
Add the following command as a new cron job:
wget -q -O https://yourdomain.com/rocket-clear-cache-cron.php >/dev/null 2>&1
Now, replace the domain name with your website URL, and also make sure that the name of the .php file is correct.
To set a proper time for this cron job, determine the exact time you want it to occur and make it in this format, for example:
30 4 * * *
This time format will execute the cron job at 4:30 AM each day. You can check this article for more examples of time formatting: https://crontab.guru/examples.html
Method 2: Setting up a WP Cron for clearing cache & preloading
If you don’t want to go too deep into setting up a cron job on the server, you may achieve similar results by installing WP Crontrol plugin.
Install this plugin and navigate to WP Admin > Tools > Cron Events > Add Cron Event.
Now select the PHP Cron event and add this code:
Now use the “Recurrence” drop-down menu and set the cron job to execute every 5 minutes and save changes.
The reason why the cache is not getting cleared could be related to a couple of things. Let’s start with the file permissions and ownership
Checking file permissions and ownership
If the cron job gets executed successfully, but the cache is not getting purged, you should check the file/folder permissions on /wp-content/cache/.
You can use FTP or cPanel File manager to check if the folder’s r/w permissions are set to 755 and if the ownership is properly set up.
If there is SSH access, we can use the following to change the permissions:
sudo chmod -R 755 /wp-content/cache
The -R flag will apply for permissions recursively.
We can also change the owner:
sudo chown -R user:group /wp-content/cache/
We just need to apply the proper user and group. If you are using cPanel, you can set the user and group to your cPanel username, for example chown -R mysite:mysite /path.
Checking cron job execution
If the cron job doesn’t get executed, please try using this command instead:
/usr/bin/php -q /home/cpanelusername/public_html/rocket-clear-cache-cron.php >/dev/null 2>&1
In this example, we will use PHP to trigger the action, and for this to work, we need to enter the absolute file path instead of the URL. Also, you need to replace the ‘cpanelusername’ with your own cPanel username.
If you are using a custom LAMP stack server, you should check the website’s absolute path and replace the upper value accordingly.
You can also use curl if the above example fails. This is the format of cron job command using curl:
curl https://yourdomain.com/rocket-clear-cache-cron.php >/dev/null 2>&1
If the cron job still fails to start, we should check the PHP error logs to see if there are any errors pointing to the cause of the issues.
Using SSH for troubleshooting
Using SSH access if available, we can use ‘crontab’ command to inspect the cron jobs settings.
Typing crontab -e will open up the cron table and we can check and see if everything is set up properly.
We can also use SSH tail and read the cron daemon logs to see what could be the cause of the potential issue.
To further help with troubleshooting, we should activate WordPress debugging by adding this code inside wp-config.php file located in the root folder.
// Enable WP_DEBUG mode define( 'WP_DEBUG', true ); // Enable Debug logging to the /wp-content/debug.log file define( 'WP_DEBUG_LOG', true );
We can access the debug.log file and check if any PHP errors are pointing to the cause of the issues.
By following the above-mentioned steps, you should be able to automate your WP Rocket and make it clear the cache, and rebuild it again at the scheduled time. If you stumbled upon any issues or have anything to add, feel free to share it in the comments below.