WP-rocket aggressive purge

In some cases WP-rocket cache plugin empties all cached files although all you do is update a post or product. Or for me when a customer does a purchase that changes the stock value for a WooCommerce product. What I can see, this doesn’t happen for me when updating a product from within WP. So it’s not consistent.

I found this is caused in rocket_get_purge_urls in inc/common/purge.php. When checking for previous and next posts to clear it sometimes finds the front page and adds that url to $purge_urls. This doesn’t only clear the cache for the actual front page, but all the cached files in that directory, i.e. the complete site!

I’ve sent this bug to the WP-rocket team, hopefully they can find a way to fix it.

But until then one way to work around this is to add a filter that always ignores the home page url. This filter doesn’t affect the domain_clean, so no worries when updating WP options or theme settings that should empty all cache.

function disable_cache_clearing_files( $urls ){
  // urls to exclude
  $exclude_urls = [ get_site_url(), get_site_url() . "/" ]; //, $_SERVER['SERVER_NAME'] ];

  // get all urls except excluded urls
  $urls = array_diff( $urls, $exclude_urls);
  return $urls;
add_filter( 'rocket_post_purge_urls', 'disable_cache_clearing_files');

The issue is also explained here: