This article describes how to delete user meta values with WordPress. It will be treated how to perform this task programmatically and with phpMyAdmin.

Introduction

Plugins and themes use user metadata to store additional information about subscribed users. For example, a WordPress membership plugin could use user metadata to store subscription settings like the selected plan, a phone number, etc.

From a developer’s perspective, multiple WordPress functions can programmatically add, edit, or remove user metadata. Specifically, you can add user meta values with the add_user_meta() function, modify existing user meta with update_user_meta() or delete user meta with delete_user_meta() and delete_metadata().

When to delete a user meta value

As a WordPress developer or administrator, you might want to delete user meta value in the following situations:

  • During the uninstall process of your plugin
  • To remove unnecessary data left by third-party plugins or themes

Where are the user metadata stored

WordPress stores user metadata in the wp_usermeta database table. Consequently, you can always visit this table to verify the updated user metadata values.

If you inspect the structure of this table, you will find the following fields:

  • umeta_id
  • user_id
  • meta_key
  • meta_value

Note that you can read the user meta programmatically with get_user_meta() or visit the wp_usermeta table with phpMyAdmin or a similar tool to verify its content after you delete metadata with delete_user_meta() or delete_metadata().

How to make the user meta visible in the user profile

Having fields in the Users -> Profile menu that reflect the value of the user metadata requires a specific implementation or the use of plugins.

To achieve this with plugins, I recommend Pods. With this simple plugin, you can complete the operation with the following procedure:

  1. Install the Pods plugin
  2. Visit the Pods Admin -> Add New menu
  3. Click on the “Add custom fields to Users” link
  4. From this screen, create all the interface elements for your user metadata. For each control, you must simply select the label, name, and field type.
  5. Click Save Pod to complete the operation.

The new field is now visible in the user profile so you can monitor the user metadata values directly from the WordPress back-end.

A user field created with Pods on the user profile page of WordPress
A user field created with Pods on the user profile page of WordPress.

Programmatically delete user metadata

You can programmatically delete user metadata using delete_user_meta() or delete_metadata(). The first is a dedicated function for deleting user meta. The latter is a more versatile function that can delete multiple types of meta, including posts, comments, terms, and users.

delete_user_meta()

User meta values are usually deleted by proving the identifier of the user and the meta key.

//Set the ID of the user for which we want to delete the meta value
$user_id = 2;

//Set the meta key used to identify the meta value
$meta_key = '_custom_user_meta';

//Delete the user meta
delete_user_meta($user_id, $meta_key);

It’s also possible to delete user metadata that matches a specific value. In this case, the third parameter should be provided.

//Set the meta value that you want to delete. By using this option, you restrict the deletion to user meta with this value.
$meta_value = '10';

//Delete the user meta
delete_user_meta($user_id, $meta_key, $meta_value);

If a plugin or theme has left unnecessary user meta values, you can easily clean up everything with this script. First, identify the key of the user meta you want to delete. Then iterate over all the users.

The script below deletes the user meta with key _plugin_x_user_meta from all the WordPress users.

//Set the meta key used to identify the meta value. In this example, it's the meta key added by a plugin.
$meta_key = '_plugin_x_user_meta';

//Get an array that includes the data of the WordPress users
$users = get_users();

//Iterate over all the users and delete the user meta with key '_plugin_x_user_meta'
foreach($users as $user){
    delete_user_meta($user->ID, $meta_key);
}

delete_metadata()

There is also an alternative to delete_user_meta(), which is delete_metadata(). This function works for posts, comments, terms, and users.

You can use it with a user by selecting the object type with its first parameters. Below is an example demonstrating how to use this function to delete a user meta value.

//Delete the meta of type 'user' of a specific user
delete_metadata( 'user', $user_id, $meta_key );

Two additional optional parameters are available for this function. Let’s see how they work.

You can target and delete only user metadata that have a specific value with the fourth parameter of this function.

//Delete the meta of type 'user' with that have '5' has a value
delete_metadata( 'user', $user_id, $meta_key, '5' );

In this example, the fourth parameter is set to null, and the fifth parameter is used to match all the entries of the specified meta key.

//Delete all the meta with the specified $meta_key. The $user_id here is ignored.
delete_metadata( 'user', $user_id, $meta_key, null, true );

Deleting user meta with phpMyAdmin

You can alternatively delete user meta with database management applications like phpMyAdmin or other.

To delete user meta with phpMyAdmin and SQL queries, proceed as follows:

  1. Log in to phpMyAdmin on your local server or from the links given by your hosting provider
  2. Select the database used to store the WordPress database tables
  3. Open the SQL tab
  4. Run the appropriate SQL query to delete user metadata. You can create simple or complex queries to target all the user metadata or only specific meta keys, users, or values.

This example SQL query deletes the user meta with key _custom_user_meta for all the users:

DELETE FROM wp_usermeta WHERE meta_key = '_custom_user_meta'

Note that you can alternatively manually delete user metadata from the interface provided by phpMyAdmin by clicking the Delete button beside the single database table rows. This functionality is available in the Browse tab on the top section of the screen.