close

wp_cache_delete( int|string $key, string $group = '' ): bool

Removes the cache contents matching key and group.

Description

See also

Parameters

$keyint|stringrequired
What the contents in the cache are called.
$groupstringoptional
Where the cache contents are grouped.

Default:''

Return

bool True on successful removal, false on failure.

Source

function wp_cache_delete( $key, $group = '' ) {
	global $wp_object_cache;

	return $wp_object_cache->delete( $key, $group );
}

Changelog

VersionDescription
2.0.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Let’s say before making an SQL request to retrieve all the data rows of a table in your database, you check for a cache and, if there isn’t one, set one.

    function wpdocs_all_rows_getter() {
    	global $wpdb;
    	$table_name = $wpdb->prefix . 'wpdocs_table';
    
    	$cache_key = 'wpdocs_cache_key';
    	$results   = wp_cache_get( $cache_key );
    
    	if ( ! $results ) {
    		$results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %s', $table_name ) );
    		// Set the cache.
    		wp_cache_set( $cache_key, $results, 'wpdocs_cache_group', 3600 );
    	}
    
    	return $results;
    }

    If you later delete a single row from the table, you’ll also have to delete the cache you set earlier to ensure data consistency.

    function wpdocs_single_row_deleter( $id ) {
    	global $wpdb;
    	$table_name = $wpdb->prefix . 'my_custom_table';
    
    	$rows_deleted = $wpdb->delete( $table_name, array( 'id' => $id ) );
    
    	if ( 0 < $rows_deleted ) {
    		$result = true;
    		// Clear the cache.
    		wp_cache_delete( 'wpdocs_cache_key', 'wpdocs_cache_group' );
    	} else {
    		$result = false;
    	}
    
    	return $result;
    }

You must log in before being able to contribute a note or feedback.