Using WordPress ‘delete_user_option()’ PHP function

The delete_user_option() WordPress PHP function deletes a user option with a global blog capability. User options are just like user metadata but they have support for global blog options. If the ‘is_global’ parameter is false, which it is by default, it will prepend the WordPress table prefix to the option name.

Usage

To delete a user option, you would use delete_user_option() like this:

delete_user_option( $user_id, 'option_name', $is_global );

This code will delete the user option named ‘option_name’ for the user with the ID of $user_id. The $is_global variable is optional and defaults to false, meaning the option is blog-specific.

Parameters

  • $user_id (int) – Required. The user ID.
  • $option_name (string) – Required. The user option name.
  • $is_global (bool) – Optional. Whether the option name is global or blog specific. Default is false (blog specific).

More information

See WordPress Developer Resources: delete_user_option

This function was implemented in WordPress version 3.0.0.

Examples

Delete a blog-specific user option

This code deletes a blog-specific user option named ‘theme_color’ for the user with ID 5.

delete_user_option( 5, 'theme_color' );
// The 'theme_color' option for user 5 is now deleted.

Delete a global user option

This code deletes a global user option named ‘language_preference’ for the user with ID 7.

delete_user_option( 7, 'language_preference', true );
// The global 'language_preference' option for user 7 is now deleted.

Check if the option was deleted

This code deletes a user option and then checks if it was successfully deleted.

$user_id = 3;
$option_name = 'display_name';

// Delete the option.
delete_user_option( $user_id, $option_name );

// Check if the option was deleted.
if ( !get_user_option( $option_name, $user_id ) ) {
    echo "Option deleted successfully.";
} else {
    echo "Failed to delete option.";
}

Delete multiple user options

This code deletes multiple user options for a user.

$user_id = 4;
$options = array( 'option1', 'option2', 'option3' );

foreach ( $options as $option ) {
    delete_user_option( $user_id, $option );
}
// The 'option1', 'option2', and 'option3' options for user 4 are now deleted.

Delete all user options for a specific blog

This code deletes all user options for all users of a specific blog.

global $wpdb;
$user_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->users" );

foreach ( $user_ids as $user_id ) {
    delete_user_option( $user_id, 'blog_option' );
}
// The 'blog_option' for all users of this blog are now deleted.