Using WordPress ‘delete_site_meta_by_key()’ PHP function

The delete_site_meta_by_key() WordPress PHP function deletes all the site metadata entries that match a given meta key.

Usage

Here’s an example of how you can use the function:

delete_site_meta_by_key('my_custom_meta_key');

In this example, all metadata entries that have a meta key of ‘my_custom_meta_key’ will be deleted.

Parameters

  • $meta_key (string – required): The metadata key to search for when deleting.

More information

See WordPress Developer Resources: delete_site_meta_by_key()

Please note that this function is not yet deprecated, and it was introduced in WordPress 3.0.0.

Examples

Deleting a Specific Site Meta Key

This example deletes a specific meta key called ‘site_theme_color’ from all site metadata.

// The meta key you want to delete
$meta_key = 'site_theme_color';

// Use the function to delete the meta key
delete_site_meta_by_key($meta_key);

This code will remove the ‘site_theme_color’ meta key from all metadata across the site.

Removing Site Meta Keys for Unused Plugins

If a plugin was removed but left meta keys behind, you can delete those keys like this:

// The meta key left by the removed plugin
$meta_key = 'removed_plugin_meta';

// Delete the meta key
delete_site_meta_by_key($meta_key);

This will ensure your site database stays clean from unused metadata.

Cleaning up Test Meta Keys

If you’ve been testing something and created a bunch of test meta keys, you can clean them up like this:

// The test meta key you created
$meta_key = 'my_test_meta_key';

// Delete the test meta key
delete_site_meta_by_key($meta_key);

This is useful for keeping your site metadata clean after testing new features or plugins.

Removing Old Site Meta Keys

If your site has been running for a long time, you might have old meta keys that are no longer used:

// An old meta key that's no longer used
$meta_key = 'old_unused_meta_key';

// Delete the old meta key
delete_site_meta_by_key($meta_key);

This can help keep your site running smoothly by reducing the size of your site’s metadata.

Deleting Meta Keys for a Specific Feature

If a feature is no longer used and you want to delete its specific meta keys:

// The meta key for the unused feature
$meta_key = 'unused_feature_meta_key';

// Delete the meta key for the unused feature
delete_site_meta_by_key($meta_key);

This will remove all metadata related to the unused feature, keeping your site clean and optimized.