'Delete user programmatically in wordpress

In Wordpress, How can I delete a user pro grammatically if I have a user ID?

I am using below code.

$user_id = 4;
$roles = array();
$user = get_userdata($user_id);
$capabilities = $user->{$wpdb->prefix . 'capabilities'};

if (!isset($wp_roles))
    $wp_roles = new WP_Roles();

foreach ($wp_roles->role_names as $role => $name) :

    if (array_key_exists($role, $capabilities))
        $roles[] = $role;

endforeach;

if (!in_array("administrator", $roles)) {
    if (wp_delete_user($user_id)) {
        echo 'User deleted' . $user_id;
        echo '<br>';
    }
}

It is not working for me. Please help me where am I wrong?



Solution 1:[1]

I have found the solution to resolve my issue.I have just added a line in code. Now updated code as given below.

require_once(ABSPATH.'wp-admin/includes/user.php' );
$user_id = 4;
$roles = array();
$user = get_userdata($user_id);
$capabilities = $user->{$wpdb->prefix . 'capabilities'};
if (!isset($wp_roles))
    $wp_roles = new WP_Roles();
foreach ($wp_roles->role_names as $role => $name) :
    if (array_key_exists($role, $capabilities))
        $roles[] = $role;
endforeach;
if (!in_array("administrator", $roles)) {
    if (wp_delete_user($user_id)) {
        echo 'User deleted' . $user_id;
        echo '<br>';
    }
}

It is now working for me.

Solution 2:[2]

Try this

$user_id                =   1;
$user_info              =   get_userdata( $user_id );
$this_user_roles        =   $user_info->roles;

//For wp_delete_user() function
require_once(ABSPATH.'wp-admin/includes/user.php' );

if( in_array( "administrator", $this_user_roles) ) {
    echo "This user is admin, cannot be deleted";
} else {
    if( wp_delete_user( $user_id ) ){
        echo "Success user deleted :)";
    } else {
        echo "There is a problem while deleting the user.";
    }
}

Solution 3:[3]

You could try the following

global $wpdb;
$ids = $wpdb->get_col('SELECT `user_id` FROM `' . $wpdb->prefix . 'usermeta` WHERE `meta_key` = \'wp_user_level\' AND `meta_value` < 8;');
if (count($ids) > 0)
{
    foreach ($ids as $id)
    {
        if (wp_delete_user($id))
        {
            echo 'User deleted' . $id;
            echo '<br>';
        }
    }
}

Use this table as reference for user levels

Solution 4:[4]

For WordPress multisite, you can remove a user quite simply from the SQL command line.

In the example below, the user_id is 838 and the site # is 20:

delete from wp_usermeta where user_id = 838 and meta_key in ('wp_20_capabilities', 'wp_20_user_level');

That's it!

Solution 5:[5]

This is what I use to delete user along with the metadata.

global $wpdb;
$user_id = "123"; // User id is 123

// Delete User metadata
$wpdb->delete($wpdb->usermeta, ['user_id' => $user_id], ['%d']);

// Delete User
$wpdb->delete($wpdb->users, ['ID' => $user_id], ['%d']);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aftab H.
Solution 2
Solution 3 Igor Yavych
Solution 4 Bret Weinraub
Solution 5 Akhtarujjaman Shuvo