delete_expired_transients( bool $force_db = false )
- Since
- 4.9.0
- Source
wp-includes/option.php:1617
Description
Note that this function won't do anything if an external object cache is in use.
The multi-table delete syntax is used to delete the transient record from table a, and the corresponding transient_timeout record from table b.
Compatibility
- WordPress
- since 4.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$force_dbbooloptional- Force cleanup to run against the database even when an external object cache is used.Default:
false
Performance profile
How much work a call to delete_expired_transients() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 7–71
- Plugin surface
- None
- Called by
- 2
Reaches the database via ->query().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 99.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- sqldatabase query
->query()called directly
Further down the call graph this can also reach hook. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 11 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost delete_expired_transients() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
wp_using_ext_object_cache() | 7 | wp_using_ext_object_cache() |
| always | 36 | ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), is_multisite() |
is_multisite() && !is_main_site() | 39 | ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), is_multisite(), is_main_site() |
!wp_using_ext_object_cache() | 39 | wp_using_ext_object_cache(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), is_multisite() |
is_multisite() && is_main_site() && !is_main_network() | 42 | ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), is_multisite(), is_main_site(), is_main_network() |
!wp_using_ext_object_cache() && is_multisite() && !is_main_site() | 42 | wp_using_ext_object_cache(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), is_multisite(), is_main_site() |
!wp_using_ext_object_cache() && is_multisite() && is_main_site() && !is_main_network() | 45 | wp_using_ext_object_cache(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), is_multisite(), is_main_site(), is_main_network() |
!is_multisite() | 59 | ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query() |
!wp_using_ext_object_cache() && !is_multisite() | 62 | wp_using_ext_object_cache(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query() |
is_multisite() && is_main_site() && is_main_network() | 68 | ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), is_multisite(), is_main_site(), is_main_network(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query() |
!wp_using_ext_object_cache() && is_multisite() && is_main_site() && is_main_network() | 71 | wp_using_ext_object_cache(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query(), is_multisite(), is_multisite(), is_main_site(), is_main_network(), ->esc_like(), ->esc_like(), time(), ->prepare(), ->query() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 99 instructions, 7–71 executed per call, 6 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 4
- wp_using_ext_object_cache()Toggles `$_wp_using_ext_object_cache` on and off without directly touching global.
- is_multisite()Determines whether Multisite is enabled.
- is_main_site()Determines whether a site is the main site of the current network.
- is_main_network()Determines whether a network is the main network of the Multisite installation.
Used by · 2
- populate_options()Create WordPress options and set the default values.
- upgrade_network()Executes network-level upgrade routines.
Source code
function delete_expired_transients( $force_db = false ) { global $wpdb; if ( ! $force_db && wp_using_ext_object_cache() ) { return; } $wpdb->query( $wpdb->prepare( "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b WHERE a.option_name LIKE %s AND a.option_name NOT LIKE %s AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) ) AND b.option_value < %d", $wpdb->esc_like( '_transient_' ) . '%', $wpdb->esc_like( '_transient_timeout_' ) . '%', time() ) ); if ( ! is_multisite() ) { // Single site stores site transients in the options table. $wpdb->query( $wpdb->prepare( "DELETE a, b FROM {$wpdb->options} a, {$wpdb->options} b WHERE a.option_name LIKE %s AND a.option_name NOT LIKE %s AND b.option_name = CONCAT( '_site_transient_timeout_', SUBSTRING( a.option_name, 17 ) ) AND b.option_value < %d", $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', time() ) ); } elseif ( is_multisite() && is_main_site() && is_main_network() ) { // Multisite stores site transients in the sitemeta table. $wpdb->query( $wpdb->prepare( "DELETE a, b FROM {$wpdb->sitemeta} a, {$wpdb->sitemeta} b WHERE a.meta_key LIKE %s AND a.meta_key NOT LIKE %s AND b.meta_key = CONCAT( '_site_transient_timeout_', SUBSTRING( a.meta_key, 17 ) ) AND b.meta_value < %d", $wpdb->esc_like( '_site_transient_' ) . '%', $wpdb->esc_like( '_site_transient_timeout_' ) . '%', time() ) ); }}Changelog
Introduced in 4.9.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/option.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.