{"id":46937,"date":"2026-10-30T09:05:13","date_gmt":"2026-10-30T09:05:13","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46937"},"modified":"2026-10-30T09:05:13","modified_gmt":"2026-10-30T09:05:13","slug":"wordpress-clear-transients-admin-bar-button","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-clear-transients-admin-bar-button\/","title":{"rendered":"WordPress Clear Transients With One Admin Bar Button"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">You need to clear transients in WordPress when a plugin keeps serving stale API data, when a debug session leaves thousands of <code>_transient_<\/code> rows in <code>wp_options<\/code>, or when a migration copied cached values from another site. WordPress already deletes *expired* transients once a day. It has no button for clearing all of them, so people end up running SQL in phpMyAdmin and forgetting the object cache.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet adds a <strong>Clear transients<\/strong> item to the admin bar for administrators. One click deletes every transient row from the options table, removes the matching entries from the options cache, and flushes the transient groups when a persistent object cache (Redis, Memcached) supports that. Then it sends you back to the page you were on, with a notice showing how many rows it removed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Put it in a small plugin, an mu-plugin or your theme&#8217;s functions.php. It also works pasted into one Scripts Organizer PHP block set to run Everywhere.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Clear all WordPress transients from the admin bar.\n *\/\n\nfunction dpc_clear_transients(): array {\n\tglobal $wpdb;\n\n\t$result = &#91; 'rows' =&gt; 0, 'cache_flushed' =&gt; false &#93;;\n\n\t\/\/ With a persistent object cache, transients live in cache groups, not the database.\n\tif ( wp_using_ext_object_cache() &amp;&amp; wp_cache_supports( 'flush_group' ) ) {\n\t\twp_cache_flush_group( 'transient' );\n\t\twp_cache_flush_group( 'site-transient' );\n\t\t$result&#91;'cache_flushed'&#93; = true;\n\t}\n\n\t\/\/ Transient keys that must survive, e.g. the cron lock.\n\t$keep = (array) apply_filters( 'dpc_clear_transients_keep', &#91; 'doing_cron' &#93; );\n\n\t$names = $wpdb-&gt;get_col(\n\t\t$wpdb-&gt;prepare(\n\t\t\t\"SELECT option_name FROM {$wpdb-&gt;options} WHERE option_name LIKE %s OR option_name LIKE %s\",\n\t\t\t$wpdb-&gt;esc_like( '_transient_' ) . '%',\n\t\t\t$wpdb-&gt;esc_like( '_site_transient_' ) . '%'\n\t\t)\n\t);\n\n\t$names = array_filter(\n\t\t$names,\n\t\tstatic function ( string $name ) use ( $keep ): bool {\n\t\t\t$key = preg_replace( '\/^_(site_)?transient_(timeout_)?\/', '', $name );\n\t\t\treturn ! in_array( $key, $keep, true );\n\t\t}\n\t);\n\n\tforeach ( array_chunk( $names, 500 ) as $chunk ) {\n\t\t$placeholders = implode( ',', array_fill( 0, count( $chunk ), '%s' ) );\n\n\t\t$result&#91;'rows'&#93; += (int) $wpdb-&gt;query(\n\t\t\t$wpdb-&gt;prepare( \"DELETE FROM {$wpdb-&gt;options} WHERE option_name IN ($placeholders)\", $chunk )\n\t\t);\n\n\t\tforeach ( $chunk as $name ) {\n\t\t\twp_cache_delete( $name, 'options' );\n\t\t}\n\t}\n\n\t\/\/ Transients without expiry are autoloaded, so drop the cached alloptions too.\n\twp_cache_delete( 'alloptions', 'options' );\n\twp_cache_delete( 'notoptions', 'options' );\n\n\treturn $result;\n}\n\nadd_action( 'admin_bar_menu', 'dpc_clear_transients_admin_bar', 100 );\nfunction dpc_clear_transients_admin_bar( WP_Admin_Bar $bar ): void {\n\tif ( ! current_user_can( 'manage_options' ) ) {\n\t\treturn;\n\t}\n\n\t$bar-&gt;add_node(\n\t\t&#91;\n\t\t\t'id'    =&gt; 'dpc-clear-transients',\n\t\t\t'title' =&gt; esc_html__( 'Clear transients', 'dpc' ),\n\t\t\t'href'  =&gt; wp_nonce_url( admin_url( 'admin-post.php?action=dpc_clear_transients' ), 'dpc_clear_transients' ),\n\t\t&#93;\n\t);\n}\n\nadd_action( 'admin_post_dpc_clear_transients', 'dpc_handle_clear_transients' );\nfunction dpc_handle_clear_transients(): void {\n\tif ( ! current_user_can( 'manage_options' ) ) {\n\t\twp_die( esc_html__( 'You are not allowed to clear transients.', 'dpc' ), 403 );\n\t}\n\n\tcheck_admin_referer( 'dpc_clear_transients' );\n\n\t$result = dpc_clear_transients();\n\t$back   = wp_get_referer() ?: admin_url();\n\n\twp_safe_redirect(\n\t\tadd_query_arg(\n\t\t\t&#91;\n\t\t\t\t'dpc_transients_cleared' =&gt; $result&#91;'rows'&#93;,\n\t\t\t\t'dpc_cache_flushed'      =&gt; (int) $result&#91;'cache_flushed'&#93;,\n\t\t\t&#93;,\n\t\t\t$back\n\t\t)\n\t);\n\texit;\n}\n\nadd_action( 'admin_notices', 'dpc_clear_transients_notice' );\nfunction dpc_clear_transients_notice(): void {\n\tif ( ! isset( $_GET&#91;'dpc_transients_cleared'&#93; ) || ! current_user_can( 'manage_options' ) ) {\n\t\treturn;\n\t}\n\n\t$message = sprintf(\n\t\t\/* translators: %d: number of deleted database rows. *\/\n\t\tesc_html__( 'Transients cleared: %d database rows deleted.', 'dpc' ),\n\t\tabsint( wp_unslash( $_GET&#91;'dpc_transients_cleared'&#93; ) )\n\t);\n\n\tif ( ! empty( $_GET&#91;'dpc_cache_flushed'&#93; ) ) {\n\t\t$message .= ' ' . esc_html__( 'Object cache transient groups flushed too.', 'dpc' );\n\t}\n\n\twp_admin_notice( $message, &#91; 'type' =&gt; 'success', 'dismissible' =&gt; true &#93; );\n}\n\n\/\/ Remove the notice arguments from the address bar after the page loads.\nadd_filter( 'removable_query_args', 'dpc_clear_transients_removable_args' );\nfunction dpc_clear_transients_removable_args( array $args ): array {\n\t$args&#91;&#93; = 'dpc_transients_cleared';\n\t$args&#91;&#93; = 'dpc_cache_flushed';\n\treturn $args;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dpc_clear_transients()<\/code> reads every option name that starts with <code>_transient_<\/code> or <code>_site_transient_<\/code>, which also covers the <code>_timeout_<\/code> rows. The underscores go through <code>$wpdb-&gt;esc_like()<\/code>, so <code>_<\/code> is matched literally and not as a wildcard.<\/li>\n\n\n<li>Rows are deleted 500 at a time with a prepared <code>IN ()<\/code> query, so a table with tens of thousands of transients needs only a few queries instead of one <code>delete_option()<\/code> call per row.<\/li>\n\n\n<li>After deleting, it clears each name from the <code>options<\/code> cache group, plus <code>alloptions<\/code> and <code>notoptions<\/code>. Transients saved without an expiry are autoloaded. If you skip this step, <code>get_transient()<\/code> can keep returning the old value for the rest of the request, or until your persistent cache expires.<\/li>\n\n\n<li>With a persistent object cache, WordPress keeps transients in the <code>transient<\/code> and <code>site-transient<\/code> cache groups, not in the database. When the cache backend supports <code>flush_group<\/code>, those two groups are flushed and the rest of the cache is left alone.<\/li>\n\n\n<li>The admin bar link points to <code>admin-post.php<\/code> and carries a nonce. The handler checks <code>manage_options<\/code> and <code>check_admin_referer()<\/code> before deleting anything, so nobody can trigger it from a forged link.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To protect a transient that is expensive to rebuild, add its key (without the <code>_transient_<\/code> prefix) to the keep list:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_filter( 'dpc_clear_transients_keep', function ( array $keep ): array {\n\t$keep&#91;&#93; = 'my_slow_api_response';\n\treturn $keep;\n} );<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer the terminal? WP-CLI does the same job and also handles the object cache:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"bash\" class=\"language-bash\"># Only the expired ones (what WordPress does daily)\nwp transient delete --expired\n\n# Everything, including site transients\nwp transient delete --all<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To run this on a schedule, call <code>dpc_clear_transients()<\/code> from your own cron hook. Daily is rarely needed, because core already deletes expired transients every day. You can check that event with <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-cli-list-cron-jobs-overdue-orphaned\/\">WP-CLI&#8217;s cron list<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clearing transients doesn&#8217;t fix a slow site by itself. The first requests afterwards are slower while plugins rebuild their caches. Use it for stale data or a bloated <code>wp_options<\/code>, not as a speed button.<\/li>\n\n\n<li>If a transient comes back seconds later, something is setting it on every request. That is often a plugin calling <code>set_transient()<\/code> with no expiry. See <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-set-transient-not-working\/\">why set_transient seems not to work<\/a> for how caching layers and expiry interact.<\/li>\n\n\n<li>On multisite this clears the current site&#8217;s options table only. Network-wide site transients live in <code>wp_sitemeta<\/code> and are left alone.<\/li>\n\n\n<li>If your object cache can&#8217;t flush a single group, the snippet doesn&#8217;t flush the whole cache for you. Flush it from your cache plugin or with <code>wp cache flush<\/code>.<\/li>\n\n\n<li>The cron lock <code>doing_cron<\/code> is kept by default, so clearing transients doesn&#8217;t start a second cron run in parallel.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For more admin bar tools, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wordpress-admin-bar-shortcuts\/\">admin shortcuts in the WordPress admin bar<\/a>.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Clear all WordPress transients from the admin bar with one click: nonce-checked, admin-only, safe with or without a persistent object cache.<\/p>\n","protected":false},"author":1,"featured_media":46938,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WordPress Clear Transients With One Admin Bar Button","description":"Clear all WordPress transients from the admin bar with one click: nonce-checked, admin-only, safe with or without a persistent object cache."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[132,3],"tags":[199,154,232,153,152],"dpc_library":[],"class_list":["post-46937","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-performance","category-wordpress","tag-admin-bar","tag-caching","tag-options","tag-performance","tag-transients"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46937"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46937\/revisions"}],"predecessor-version":[{"id":46939,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46937\/revisions\/46939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46938"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46937"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46937"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}