In a previous post I explained that clearing PHP opcode caches before WordPress Updates helps in streamlining the update process. WordPress updates no longer fail because of cached file locations. Did you know you can automatically flush opcode caches like Redis when you publishing a post or page in WordPress? Doing so ensures you and your visitors see the newly created content immediately.

You can use the following PHP script as a WordPress Must Use Plugin. It'll "fire" on the publish_post action, deleting all cache in Redis whenever you publish a post. Herein lies it's disadvantage: all caches are flushed, and not just that of the home page for instance.

Important notes follow below the code.

Flush Redis cache after post publish in WordPress
Save the following PHP code as a plugin in a new file called, for example, flush-redis.php and upload the file to your wp-content/mu-pluginsfolder.

 * Plugin Name: Flush Redis cache
 * Plugin URI:
 * Donate URI:
 * Description: Flushes PHP Redis cache upon publish_post action. By clearing the opcode cache in memory you immediately can see a new or changed post.
 * Network: True
 * Version: 1.2
 * Author: Jan Reilink
 * Author URI:
 * License: GPLv2

require_once( ABSPATH .'/wp-config.php' );
function manual_clear_redis_cache() {
	if( class_exists( 'Redis' ) ) {
		$r = new Redis();
		if ($r->connect( WP_REDIS_PATH, 0 )) {
			if( false === $r->flushAll() ) {
				if( WP_DEBUG === true ) {
					error_log( 'After post publish: Flushing PHP Redis failed.' );
				return false;
			else {
				if( WP_DEBUG === true ) {
					error_log( 'After post publish: PHP Redis flushed succesfully.' );
				return true;
		else {
			if( WP_DEBUG === true ) {
				error_log( 'Could not connect to PHP Redis.' );
add_action( 'publish_post', 'manual_clear_redis_cache', 10, 2 );

// frc = flush redis cache
add_filter( 'plugin_row_meta', 'frc_plugin_row_meta', 10, 2 );
function frc_plugin_row_meta($links, $file) {
	if ( !preg_match('/flush-redis.php$/', $file ) ) {
		return $links;

	$links[] = sprintf(
		'<a target="_blank" href="" title="Donate to Jan Reilink / Sysadmins of the North">%s</a>',
		__( 'Donate' )
	return $links;

Important notes and caveats:

  1. this must use plugin to flush Redis cache works perfectly in my situation, it may not in yours.
  2. the plugin relies on a defined value WP_REDIS_PATH, in my case a Unix file-socket. Other Redis opcode cache plugins, like Redis Object Cache, require these connections parameters in wp-config.php, I found it easy to reuse it. If you're not using a plugin like Redis Object Cache, then add the connection parameters to your wp-config.php anyway, or you may need to change the connect() function.
  3. all cached data in Redis is deleted using a flushAll()function.
  4. success and failure are logged to WP_DEBUG_LOG, if WP_DEBUG is enabled. Otherwise the plugin fails or succeeds silently.
  5. other plugins may or may not have different functionality to flush Redis caches.

Please let me know what you think of this little Redis cache busting plugin. If you're using wp-redis by Pantheon, they offer you two alternative cache flushing solutions on their Wiki.

Donate a cup of coffee
Donate a cup of coffee

Thank you very much! <3 ❤️


  1. Chris

    Where does this `Redis` class originate that has the `flushAll()` method mentioned?

  2. Hi there,

    for me this is not working, ay clue why? Woueld it be possible to flush just the Mainpage (Home) as there are my new Posts showing up.

    • Hi Thomas,
      WP_DEBUG’s debug.log should give you some information whether the flush is performed or not. Without any more information I can’t even start to guess why it’s not working for you. There are a lot of Redis plugins available for WordPress, all with their own configuration. My MU-plugin relies on a defined value WP_REDIS_PATH, containing the path to a unix socket. This must have defined in your wp-config.php. This code deletes all Redis cache on success by using the flushAll() function. You could change the code to use or

      Hope this helps, good luck!

  3. ems

    why not use the flushdb instead?

    • Using flushdb is another, and great, option. Thanks. The Redis flushall command just did what I wanted to achieve.

Comments are closed