How to programmatically clear the cache and optimizations

This article shows how can the cache and optimizations be purged programmatically, to extend or customize the default behavior of WP Rocket, and some examples of usage for each.

Heads up! This guide assumes comfort with editing PHP code. If that's not you, please ask your developer to implement it for you.

Delete all cache files

You can use the rocket_clean_domain() function to clear the whole cache folder, linked to the page caching feature.

For example, you can clear the WP Rocket cache folder when a plugin is updated and when the WordPress core is updated using this snippet.

Delete the cache of a post

You can use the rocket_clean_post() function to clear the cache of a post, and its related content.

For example, this snippet will clear the post cache after saving a page using the Breakdance builder.

Delete only specific cache files

You can use the rocket_clean_files() function to clear the cache file only for a specific URL(s).

Here's an implementation of this function used to clear the cache of some specific URLs after a vote is casted using the TotalPoll plugin.

Delete Used CSS

You can call the truncate_used_css() method within a PHP function to clear the Used CSS (Remove Unused CSS option).

As an example, you can refer to this helper plugin which clears the Used CSS and the cache after the theme is updated.

Delete minified files

The rocket_clean_minify() function will clear all the minified files in the /wp-content/cache/min/ folder, which are generated by the Minify CSS files and Minify JavaScript files optimizations.

For example, with this snippet, the cache of minified files is cleared when an Elementor draft is saved.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.