How to Clear WP Fastest Cache with PHP Functions
In this article
Clearing WP Fastest Cache programmatically via PHP functions is useful when you need to purge the cache after custom code changes, plugin updates, or during automated deployment workflows. This guide shows you the exact PHP functions to use, how to call them safely, and what to avoid.
What You'll Need
- A WordPress site with WP Fastest Cache plugin installed and active.
- Access to your theme's
functions.phpfile or a custom plugin file (recommended). - Basic knowledge of PHP and WordPress hooks.
- FTP or file manager access to edit files (or use a code snippet plugin).
Step-by-Step
- Identify the correct cache clearing method. WP Fastest Cache provides a public static method:
WpFastestCache::deleteCache(). This clears all cached pages, minified CSS/JS, and combined files. For a safer, more targeted approach, you can also useWpFastestCache::deleteCache(true)to clear only the cache folder without touching the database-optimized files. - Check if the class exists. Before calling any method, verify the plugin is active and the class is available. This prevents fatal errors if the plugin is deactivated or updated. Use:
if (class_exists('WpFastestCache')) { ... } - Create a helper function. In your theme's
functions.phpor a custom plugin, add a function like:function my_clear_wpfc_cache() {
if (class_exists('WpFastestCache')) {
$wpfc = new WpFastestCache();
$wpfc->deleteCache();
}
} - Hook the function to a custom action. To trigger the cache clear programmatically, add an action hook. For example, after saving a custom post type, you can call it:
add_action('save_post_my_cpt', 'my_clear_wpfc_cache');Or create a custom admin-ajax endpoint for manual triggering. - Test the function. After adding the code, reload a cached page, then trigger the hook (e.g., save a post). Check if the page's cache is cleared by viewing the page source or using a cache-checking tool. Verify the cache folder is emptied via FTP.
- Use a transient or flag to avoid repeated clears. If your hook fires multiple times (e.g., multiple saves), add a static variable or transient to ensure the cache is cleared only once per request:
if (!get_transient('my_cache_clear_done')) { ... set_transient('my_cache_clear_done', 1, 5); } - Handle multisite carefully. On WordPress Multisite, the cache is per-site. Use
switch_to_blog()if you need to clear a specific site's cache, and always callrestore_current_blog()after.
Common Mistakes
- Calling the method without checking class existence. This causes a fatal error if the plugin is disabled.
- Using
deleteCache(true)when you want full clear. The boolean parameter changes behavior; read the plugin source to understand exactly what it does. - Clearing cache on every page load. This defeats the purpose of caching and slows down your site. Only clear when content changes.
- Forgetting to include the plugin's main file. If you call the function before the plugin is loaded, the class won't exist. Use the
plugins_loadedhook or check inside your function. - Directly editing the plugin's core files. This is overwritten on updates and can break the plugin. Always use a custom function in your theme or a separate plugin.
Tips
- Use a custom plugin instead of functions.php. This keeps your code safe from theme updates and allows easier maintenance.
- Log the cache clear action. Add
error_log('Cache cleared at ' . date('Y-m-d H:i:s'));to debug when the function runs. - Combine with a cron job. Schedule a WordPress cron event to clear the cache during off-peak hours if you have frequent content updates.
- Consider using the plugin's built-in API. WP Fastest Cache also offers a REST API endpoint (if enabled) for cache clearing, which might be simpler for external scripts.
- Test on a staging site first. Always verify the function works without errors before deploying to production.
Troubleshooting
- Function does nothing. Check that the plugin is active and the class name is correct. Look for typos in the class name:
WpFastestCache(capital W, F, C). Also check your PHP error log for warnings. - Fatal error: Class not found. This means the plugin is not loaded at the time your function runs. Wrap your call in a
plugins_loadedaction or checkclass_existsbefore calling. - Cache clears but immediately regenerates. This is normal if you have a caching plugin that preloads. Check your preload settings and disable preloading if you want to control when cache is rebuilt.
- Only part of the cache is cleared. WP Fastest Cache stores multiple types (page cache, minified CSS/JS, object cache). The
deleteCache()method should clear all, but if not, also call$wpfc->deleteCache(true)or manually delete the cache folder via FTP. - Multisite: wrong site's cache cleared. Use
switch_to_blog($site_id)before calling the function andrestore_current_blog()after. - Performance issues after adding the function. If your function runs on every page load, it can slow down the site. Add a conditional check, e.g., only run on specific admin pages or after specific events.
Frequently Asked Questions
Can I use wp_cache_flush() instead?
No, wp_cache_flush() clears WordPress's object cache, not WP Fastest Cache's page cache. Use the plugin's own method.
Is it safe to call deleteCache() on every post save?
Yes, but it may cause performance spikes if you have a large site. It's better to clear only when necessary, e.g., after a post is published, not on every autosave.
How do I clear cache for a specific page?
WP Fastest Cache does not have a built-in method to clear a single page via PHP. You can delete the specific cache file from the cache folder manually, but the plugin's method clears all.
Will this work with other caching plugins?
No, this is specific to WP Fastest Cache. Other plugins have their own functions.
Can I call this function from an external script?
Yes, but you need to load WordPress first by including wp-load.php from the site root, then call the function. Be careful with security—only allow authenticated requests.
What if the plugin updates and the method changes?
The method has been stable for years, but always check the plugin's changelog. If it changes, update your custom function accordingly.