Using WordPress ‘delete_site_meta()’ PHP function

The delete_site_meta() WordPress PHP function removes metadata from a site based on specific criteria.

Usage

Here’s a basic usage of the function where we delete a particular meta data from a site:

delete_site_meta( $site_id, 'meta_key_to_remove', 'meta_value_to_remove' );

In this example, meta_key_to_remove is the key of the metadata you want to remove, and meta_value_to_remove is the value of the metadata. This will remove all metadata that matches both the key and value.

Parameters

  • $site_id (int): This is the ID of the site from which you want to remove the metadata.
  • $meta_key (string): This is the key of the metadata you want to remove.
  • $meta_value (mixed): This is the value of the metadata you want to remove. If provided, only metadata rows that match this value will be removed. It must be serializable if non-scalar. This parameter is optional and defaults to ”.

More Information

See WordPress Developer Resources: delete_site_meta()

This function is not deprecated and is available in the WordPress core.

Examples

Delete Site Metadata with Key

Here we delete metadata from site 3 with a meta key ‘page_color’:

delete_site_meta( 3, 'page_color' );

This will remove all metadata with the key ‘page_color’ from site 3.

Delete Site Metadata with Key and Value

In this case, we delete metadata from site 3 with a meta key ‘page_color’ and meta value ‘blue’:

delete_site_meta( 3, 'page_color', 'blue' );

This will remove all metadata with the key ‘page_color’ and value ‘blue’ from site 3.

Delete Site Metadata with Serialized Value

If the metadata value is non-scalar, it must be serialized. Here we delete metadata from site 3 with a meta key ‘page_features’ and a serialized meta value:

delete_site_meta( 3, 'page_features', serialize(array('search', 'archives')) );

This will remove metadata with the key ‘page_features’ and value of the serialized array from site 3.

Delete Site Metadata without Providing Meta Value

If the meta value is not provided, all metadata that matches the key will be removed. Here we delete metadata from site 3 with a meta key ‘page_color’:

delete_site_meta( 3, 'page_color' );

This will remove all metadata with the key ‘page_color’ from site 3, regardless of the value.

Delete Site Metadata with Empty Meta Value

If the meta value is an empty string, it behaves the same as not providing the meta value. Here we delete metadata from site 3 with a meta key ‘page_color’:

delete_site_meta( 3, 'page_color', '' );

This will remove all metadata with the key ‘page_color’ from site 3, regardless of the value.