On this page
delete_expired_transients( bool $force_db = false )
Deletes all expired transients.
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.
Parameters
$force_dbbool Optional-
Force cleanup to run against the database even when an external object cache is used.
Default:
false
Source
File: wp-includes/option.php. View all references
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()
)
);
}
}
Related
Uses
| Uses | Description |
|---|---|
| wpdb::esc_like() wp-includes/class-wpdb.php | First half of escaping for |
| wp_using_ext_object_cache() wp-includes/load.php | Toggle |
| is_main_site() wp-includes/functions.php | Determines whether a site is the main site of the current network. |
| is_main_network() wp-includes/functions.php | Determines whether a network is the main network of the Multisite installation. |
| wpdb::query() wp-includes/class-wpdb.php | Performs a database query, using current database connection. |
| is_multisite() wp-includes/load.php | If Multisite is enabled. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| populate_options() wp-admin/includes/schema.php | Create WordPress options and set the default values. |
| upgrade_network() wp-admin/includes/upgrade.php | Executes network-level upgrade routines. |
Changelog
| Version | Description |
|---|---|
| 4.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/delete_expired_transients