Using WordPress ‘delete_site_option()’ PHP function

The delete_site_option() function is a WordPress PHP function used to remove a specific option by its name for the current network.

Usage

To use this function, you need to provide the name of the option you wish to delete. For instance, if you have an option named ‘custom_option’, you would delete it like this:

if ( delete_site_option( 'custom_option' ) ) {
    echo "Custom option deleted successfully.";
}

If the option was successfully deleted, it will output “Custom option deleted successfully.”

Parameters

  • $option (string): This is the name of the option to delete. It is expected to not be SQL-escaped.

More information

See WordPress Developer Resources: delete_site_option()
This function is part of WordPress core and is not depreciated as of the last update. For more detailed information and the source code, visit the link provided above.

Examples

Deleting a simple site option

This example demonstrates how to delete a site option named ‘theme_color’.

if ( delete_site_option( 'theme_color' ) ) {
    echo "Theme color option deleted.";
}

Deleting a site option within a conditional

Here we first check if an option ‘site_logo’ exists before trying to delete it.

if ( get_site_option( 'site_logo' ) ) {
    if ( delete_site_option( 'site_logo' ) ) {
        echo "Site logo option deleted.";
    }
}

Using the function in a plugin

In this example, we’re creating a simple plugin that deletes a site option ‘plugin_data’ upon deactivation.

function myplugin_deactivate() {
    if ( delete_site_option( 'plugin_data' ) ) {
        echo "Plugin data option deleted.";
    }
}
register_deactivation_hook( __FILE__, 'myplugin_deactivate' );

Deleting multiple site options

This example demonstrates how to delete multiple site options.

$site_options = array('option_1', 'option_2', 'option_3');

foreach ($site_options as $option) {
    if ( delete_site_option( $option ) ) {
        echo "Deleted option: $option";
    }
}

Handling the case when the option deletion fails

In this example, we are handling the case when the deletion of the ‘admin_notice’ option fails.

if ( ! delete_site_option( 'admin_notice' ) ) {
    echo "Failed to delete admin_notice option.";
} else {
    echo "Admin notice option deleted.";
}