Using WordPress ‘clean_network_cache()’ PHP function

The clean_network_cache() WordPress PHP function removes a network or multiple networks from the object cache.

Usage

To use the clean_network_cache() function, you simply need to pass the ID of the network or an array of network IDs that you want to remove from the cache.

clean_network_cache( $ids );

Let’s say we have two networks with IDs 1 and 2, and we want to remove both of them from the cache. Here is how you can do it:

clean_network_cache( array(1, 2) );

Parameters

  • $ids (int|array): Required. The Network ID or an array of Network IDs to remove from the cache.

More information

See WordPress Developer Resources: clean_network_cache()

Examples

Removing a Single Network from Cache

This example removes a network with the ID of 1 from the object cache.

// Remove network with ID 1 from cache
clean_network_cache(1);

Removing Multiple Networks from Cache

This example removes networks with IDs 1, 2, and 3 from the object cache.

// Remove networks with IDs 1, 2, 3 from cache
clean_network_cache(array(1, 2, 3));

Removing All Networks from Cache

This example removes all networks from the object cache by getting all network IDs first.

// Get all network IDs
$network_ids = get_all_network_ids();

// Remove all networks from cache
clean_network_cache($network_ids);

Cleaning Cache after Creating a Network

After creating a new network, you might want to clean the cache to ensure the new network is not cached yet.

// Create a new network
$network_id = wp_create_network('Network Name', 'http://example.com');

// Remove the new network from cache
clean_network_cache($network_id);

Cleaning Cache after Deleting a Network

After deleting a network, it’s a good practice to clean the cache to remove any traces of the deleted network.

// Delete a network
wp_delete_network(1);

// Remove the deleted network from cache
clean_network_cache(1);