Home » Functions » wp_delete_user

Function Name: wp_delete_user

Explanation: The wp_delete_user function is a powerful WordPress function that allows you to delete a user from your WordPress website. This function comes in handy when you need to remove a user account and all associated data from your site.

What it does: The wp_delete_user function performs a series of actions to completely remove a user from the WordPress database. When called, it deletes the user’s account, including their username, email address, and any other personal information stored in their user profile. Additionally, this function also deletes all content created by the user, such as posts, pages, comments, and media attachments. It also reassigns any content associated with the user to another specified user, ensuring that no content is lost during the deletion process.

What it’s used for: The wp_delete_user function is commonly used in scenarios where a user account needs to be permanently removed from a WordPress website. This can be necessary for a variety of reasons, such as when a user requests to have their account deleted, when dealing with spam or fake user accounts, or when performing site maintenance and cleanup.

Example usage code: Here’s an example of how you can use the wp_delete_user function in your WordPress code:

$user_id = 123; // ID of the user you want to delete
$reassign_user_id = 456; // ID of the user to reassign content to (optional)

$result = wp_delete_user( $user_id, $reassign_user_id );

if ( is_wp_error( $result ) ) {
    echo 'User deletion failed: ' . $result->get_error_message();
} else {
    echo 'User successfully deleted!';

In the above example, we first define the ID of the user we want to delete and, optionally, the ID of a user to reassign the content to. We then call the wp_delete_user function, passing the user ID and reassignment ID as parameters. The function returns a result, which can be an instance of the WP_Error class if an error occurs. We can then check the result and display an appropriate message based on the outcome of the deletion process.

Note: It’s important to exercise caution when using the wp_delete_user function, as it permanently deletes user data and content from the database. Make sure to take proper backups and verify the user and reassignment IDs before executing this function.

Learn More on WordPress.org

WordPress snippets using the wp_delete_user function

Register an account to save your snippets or go Pro to get more features.