WP_CLI\Utils

wp_clear_object_cache()WP-CLI 1.0

Deprecated from version 1.5.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.

Clear WordPress internal object caches.

In long-running scripts, the internal caches on $wp_object_cache and $wpdb can grow to consume gigabytes of memory. Periodically calling this utility can help with memory management.

No Hooks.

Return

null. Nothing (null).

Usage

wp_clear_object_cache();

Changelog

Deprecated since 1.5.0

wp_clear_object_cache() code WP-CLI 2.8.0-alpha

function wp_clear_object_cache() {
	global $wpdb, $wp_object_cache;

	$wpdb->queries = [];

	if ( ! is_object( $wp_object_cache ) ) {
		return;
	}

	// The following are Memcached (Redux) plugin specific (see https://core.trac.wordpress.org/ticket/31463).
	if ( isset( $wp_object_cache->group_ops ) ) {
		$wp_object_cache->group_ops = [];
	}
	if ( isset( $wp_object_cache->stats ) ) {
		$wp_object_cache->stats = [];
	}
	if ( isset( $wp_object_cache->memcache_debug ) ) {
		$wp_object_cache->memcache_debug = [];
	}
	// Used by `WP_Object_Cache` also.
	if ( isset( $wp_object_cache->cache ) ) {
		$wp_object_cache->cache = [];
	}
}