How to clear cache via cron job

If you want to make sure that cache is cleared at a certain time every day, you can 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.

Clear cache

In order to set up your custom cron job, create a PHP file and name it (for example) rocket-clean-domain.php. Paste the following code inside:

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

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

Place this file in your WordPress installation's root (where wp-config.php and wp-load.php are located).

Note: If you place it on some other place, you need to edit path in require( 'wp-load.php' ); above to match its location.

Clear cache and trigger preload

The snippet above will only clear the cache, but not preload it. In order to trigger a preload, use run_rocket_sitemap_preload(). Make sure a sitemap URL is set in your WP Rocket options.

<?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();
}

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