How to Clear Cache Via Cron Job? Top 3 Methods Explained

Cron is one of the most useful utilities that you’ll find in any Unix-like operating system. Usually, it is used to schedule commands at a specific time, and these tasks or schedule commands are known as Cron jobs.

Some people get confused between WP-Cron and cron jobs, but both are different. WP-Cron isn’t actually a real cron job; you can say it is a “fake cron job”. WP-Cron has several drawbacks; therefore, it is recommended to set up a real cron job on your server to clear cache instead of relying on WP-Cron.

But, do you know how to clear cache via cron job? If the answer is No, then keep reading this guide. Here, I’ll share how to set up a cron job and clear cache using it.

How to Set Up a Cron Job?

To create a real cron job, you need to disable the WP-Cron first. By doing this, it is not executed every time someone loads one of your pages. So, to disable WP-Cron-

  • Open the wp-config.php file in your main WordPress folder.
  • Then, add the following command before “/”.
define('DISABLE_WP_CRON', true);

Then, make a cron job entry through your web hosting control panel. Here, you can set the job to run every 5 or 10 minutes. So, here’s how to do it-

  • Log in to your cPanel, and navigate to ADVANCED to find Cron Jobs. Also, you can search by typing cron jobs to directly find it.
  • Click on the cron jobs, go to “Common Setting”, and select “once Per Five Minutes(*/5****), or any other option you want.

How to Set Up a Cron Job setting

  • Then, paste the following command into the command section.
wget -q -O - http://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
  • Make sure, replacing yourdomain.com with your actual domain.

setup cache via cron job replacing yourdomain.com with your actual domain

  • Lastly, click on Add New Cron Job, and that’s all.

Keep in mind that each server works differently to add a cron job, so, with any confusion, ask your hosting provider to help you.

How to Clear Cache Via Cron Job?

How to Clear Cache Via Cron Job

Once the cron job setting is done, now it’s time to know how to clear cache via cron job. Here, I’ll share three methods for caching your site. So, let’s get into it.

Clear the Entire Site’s Cache

You can use the WP Rocket WordPress plugin and extend the functionality using cron to launch custom functions. To clear cache for the entire site-

  • First, create a PHP file and name it like- rocket-clean-domain.php.
  • Then use the following code in your file.
<?php 
// Load WordPress.
require( 'wp-load.php' );

// Clear cache.
if ( function_exists( 'rocket_clean_domain' ) ) {
	rocket_clean_domain();
 }
  • After that, upload this file to your WordPress installation’s root. You’ll find it where the wp-config.php and wp-load.php are located. With any confusion, you can see this video.

If you make any mistake by placing it in a different location, edit the path in require( ‘wp-load.php’ ); above to match its location.

  • Then, specify the correct path to your new file rocket-clean-domain.php in your cron job setting.

Clear Cache and Trigger Preload

From the above code, you can only clear the cache without preloading it. But, with the following code snippet, you can clean the cache of a specific page as well as preloading the cache.

So, use run_rocket_sitemap_preload() in order to trigger a preload. The sitemap preloading should be enabled in the WP Rocket settings. If you’re using other plugins, make sure the auto-detection is enabled, or the sitemap URL is specified.

So, to clear cache and trigger preload, follow the below command-

<?php 
// Load WordPress.
require( 'wp-load.php' );

// Clear cache.
if ( function_exists( 'rocket_clean_domain' ) ) {
	rocket_clean_domain();
}

// Preload cache.
if ( function_exists( 'run_rocket_sitemap_preload' ) ) {
	run_rocket_sitemap_preload();
}

To trigger only the homepage-based preloading, use run_rocket_bot in place of run_rocket_sitemap_preload. As I said before, the preload option should be enabled in the WP Rocket.

Clear Cache of A Specific Page

If you want to clear a specific page’s cache instead of the entire cache, make sure to-

  • Replace https://example.com/page_url_1 with the specific URL you want to clear and preload.
  • Copy and edit the line for all the URLs you want to clear and preload.
  • Then, clear the specific URLs with the following code snippet.
<?php

// Load WordPress.
require( 'wp-load.php' );

define( 'WP_USE_THEMES', false );

// Add one page/post per line.
$pages_to_clean_preload = [
		'https://example.com/page_url_1',//copy this line as many times as necessary.
		'https://example.com/page_url_2',//copy this line as many times as necessary.
		];

if ( function_exists( 'rocket_clean_post' ) ) {

	foreach( $pages_to_clean_preload as $page_to_clean) {
		rocket_clean_post( url_to_postid ( $page_to_clean ) );
	}
}

if ( function_exists( 'get_rocket_option' ) ) {
	
	if( 1 == get_rocket_option( 'manual_preload' ) ) {
		
		$args = array();

		if( 1 == get_rocket_option( 'cache_webp' ) ) {
			$args[ 'headers' ][ 'Accept' ]      	= 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
			$args[ 'headers' ][ 'HTTP_ACCEPT' ] 	= 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8';
		}
	
		// Preload desktop pages/posts.
		rocket_preload_page( $pages_to_clean_preload, $args );
		
		if( 1 == get_rocket_option( 'do_caching_mobile_files' ) ) {
			$args[ 'headers' ][ 'user-agent' ] 	= 'Mozilla/5.0 (Linux; Android 8.0.0;) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Mobile Safari/537.36';
		
			// Preload mobile pages/posts.
			rocket_preload_page(  $pages_to_clean_preload, $args );
		}
  	}
}

function rocket_preload_page ( $pages_to_preload, $args ){
	
	foreach( $pages_to_preload as $page_to_preload ) {
		wp_remote_get( esc_url_raw ( $page_to_preload ), $args );
	}
}

Conclusion

Generally, Cron Jobs are used to scheduling tasks to run on the server and automate system maintenance or administration. Though they seem like tools, they are actually relevant to many kinds of web applications or development.

However, you can expire and clean cached data files at a certain interval with cron jobs. In this guide, I’ve shown how to clear cache via cron job. Hopefully, you’ve found this guide helpful.

Please leave your comments and questions regarding this topic in the comment section below.

Leave a Comment