# Delete Expired Transients From the Admin Bar

> Adds a toolbar button for administrators that removes expired transients from the database in one click, keeping the options table lean.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: database, transients, cleanup, admin-bar
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-clear-expired-transients/

## Code

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

    $bar->add_node( array(
        'id'     => 'wpide-clear-transients',
        'parent' => 'top-secondary',
        'title'  => 'Clear expired transients',
        'href'   => wp_nonce_url( admin_url( 'admin-post.php?action=wpide_clear_transients' ), 'wpide_clear_transients' ),
    ) );
}, 999 );

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

    check_admin_referer( 'wpide_clear_transients' );

    delete_expired_transients( true );

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

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

## Notes

Only expired transients are deleted, so cached data that is still valid is kept.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Delete Expired Transients 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/
