# Flush the Object Cache From the Admin Bar

> Adds a toolbar button for administrators that empties the object cache (Redis, Memcached or the built-in cache) after deployments or data fixes.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: cache, object-cache, admin-bar, deployment
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-flush-object-cache-button/

## Code

```php
add_action( 'admin_bar_menu', function ( $bar ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    $bar->add_node( array(
        'id'     => 'wpide-flush-cache',
        'parent' => 'top-secondary',
        'title'  => wp_using_ext_object_cache() ? 'Flush object cache' : 'Flush cache',
        'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=wpide_flush_cache' ), 'wpide_flush_cache' ),
    ) );
}, 999 );

add_action( 'admin_post_wpide_flush_cache', function () {
    if ( ! current_user_can( 'manage_options' ) ) {
        wp_die( 'Sorry, you are not allowed to do that.' );
    }

    check_admin_referer( 'wpide_flush_cache' );

    wp_cache_flush();

    wp_safe_redirect( add_query_arg( 'wpide_cache_flushed', '1', wp_get_referer() ? wp_get_referer() : admin_url() ) );
    exit;
} );

add_action( 'admin_notices', function () {
    if ( isset( $_GET['wpide_cache_flushed'] ) && current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification
        echo '<div class="notice notice-success is-dismissible"><p>Object cache flushed.</p></div>';
    }
} );
```

## Notes

Flushing a shared persistent cache affects every site using it; on multisite it clears the whole network's cache.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Flush the Object Cache From the Admin Bar" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
