How to clear cache via cron job

WP Rocket provides custom functions for clearing and preloading the cache. In order to run these at specific times each day, you can use a cron job on your server.

In this Article

Set Up a Cron Job

  1. If you want to make sure that the cache is cleared at a certain time every day, you must first create a cron job through your web hosting control panel (check this video as a quick guide) and launch it at whatever time you want.
  2. Instead of pointing to wp-cron (as in the tutorial above), you will set your cron job to point to the specific file you create and upload in the next step. Make sure to specify the correct path to your custom file, rocket-clean-domain.php, in your cron job settings.

Clear and preload cache

  1.  Create a PHP file and name it (for example): rocket-clean-domain.php
  2. To clear the cache and preload the whole site, use the following code in your file:
  3. <?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();
    }
    	
  4. Upload this file to your WordPress installation's root ( where wp-config.php and wp-load.php are located).
  5. Note: If you place it in a different location, you need to edit the path in require( 'wp-load.php' ); above to match its location.

  6. Make sure to specify the correct path to your new file rocket-clean-domain.php in your cron job settings.

Note: if the Preload feature is enabled, the rocket_clean_domain() function will also trigger the preloading process right after the cache was cleared. 

Clear and Preload Specific Page(s)

Instead of clearing and preloading the whole cache, you can clear and preload a specific URL(s) with the following code snippet. 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.
<?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 );
	}
}<br>

Add custom PHP snippets to WP Cron using WP Crontrol plugin

As an alternative method of running PHP snippets as cron events, you can use the WP Crontrol plugin.
Please follow these steps to add a new cron event:

  1. Install WP Crontrol plugin
  2. Inside WP Admin, go to Tools -> Cron Events -> Add Cron Event. 
  3. Choose PHP Cron event
  4. Add your code snippet. The opening <?php tag, and the require( 'wp-load.php' ); line must not be included in these types of snippets.
  5. Choose the recurrence, and save the event.

Note: To improve the reliability of WordPress scheduled events in general, is recommended to add a server-side cron job to trigger wp-cron.php, You can follow the steps described in our doc: Setting up a real cron job

Code Snippets for WP Rocket versions prior 3.12

Heads up! The following is obsolete information, which is only relevant to WP Rocket 3.11.5 and older.

You may use one of the following code snippets in case you want to also preload the cache, or clear a specific page instead of the whole cache.

Clear cache and trigger preload

In the example above the code provided will only clear the cache, but not preload it. In order to trigger a preload, use run_rocket_sitemap_preload(). Make sure Sitemap preloading is enabled in the WP Rocket settings and that either auto-detection (for Yoast or other plugins) is enabled, or a sitemap URL is specified.

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

// Clear cache.
// Only available on WP Rocket versions older than 3.12
if ( function_exists( 'rocket_clean_domain' ) ) {
	rocket_clean_domain();
}

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

If you want to trigger only the homepage-based preloading instead, you can use run_rocket_bot in place of run_rocket_sitemap_preload. Make sure that the preload option is enabled in WP Rocket.

If you want to make sure that there is no automatic cache clearing and preloading besides that triggered by your cron job, you should also: 

Clear and Preload a Specific Page(s)

Instead of clearing the whole cache, you can clear a specific URL(s) with the following code snippet. 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.
<?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 );
	}
}
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.