Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Remove all users with no role 'None' WordPress

  • SOLVED

Hello,
I've got a client with over +50K spam users who've signed up through a forum that they never really used by real users. Code/plugin suggestion to successfully remove them by bulk.
http://screencast.com/t/eCchUeTBEtKJ

Any suggestions?

Answers (5)

2013-09-22

zebra webdesigns answers:

Hello Viracreatv

First thing take the backup of your database before deleting anything. and the second thing I can help you through teamviewer to resolve this issue or you can send the database file to [email protected]

If you dont want to take any risk then download Team viewer http://www.teamviewer.com/hi/index.aspx
and ping me, through my skype id bhuvan530531
so that you can see what I am doing.


zebra webdesigns comments:

Hello Vira as discussed I have PM the working code.
It will do the work.

There are lot more cleaning work are there.
Once you are ok with this code we can proceed

2013-09-22

Agus Setiawan answers:

i can do this through phpmyadmin quickly


Agus Setiawan comments:

you can use this plugin, deleting user by role : http://wordpress.org/plugins/bulk-delete/


Creativira comments:

Thanks for the suggestion. Will try the plugin. I've stumble upon it before but wasn't convince that it will delete a user with a None role.


Creativira comments:

Hello,
It was as I suspected, it does not have an option to remove a user with a 'None' role. Any other ideas?
http://screencast.com/t/Koj9hCjxfx

2013-09-22

Navjot Singh answers:

Add this code to your theme's functions.php

function remove_subscribers() {
global $wpdb;
$args = array( 'role' => 'Subscriber' );
$subscribers = get_users( $args );
if( !empty($subscribers) ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
$i = 0;
foreach( $subscribers as $subscriber ) {
if( wp_delete_user( $subscriber->ID ) ) {
$i++;
}
}
echo $i.' Subscribers deleted';
} else {
echo 'No Subscribers deleted';
}
}
remove_subscribers();


and visit your site once. Then remove the code. Change the role of subscriber to one which you want to delete.

Source: http://www.shanestrong.com/wordpress/bulk-delete-wordpress-spam-users


Creativira comments:

Navjot,
To be clear, what I will be deleting is 'None' which is not really a role.
From your cold, I will use this:

function remove_subscribers() {

global $wpdb;

$args = array( 'role' => 'Subscriber' );

$subscribers = get_users( $args );

if( !empty($subscribers) ) {

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

$i = 0;

foreach( $subscribers as $subscriber ) {

if( wp_delete_user( $subscriber->ID ) ) {

$i++;

}

}

echo $i.' Subscribers deleted';

} else {

echo 'No Subscribers deleted';

}

}

remove_subscribers();


Is that correct?


Creativira comments:

*code not cold :)


Creativira comments:

function remove_subscribers() {

global $wpdb;

$args = array( 'role' => 'None' );

$subscribers = get_users( $args );

if( !empty($subscribers) ) {

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

$i = 0;

foreach( $subscribers as $subscriber ) {

if( wp_delete_user( $subscriber->ID ) ) {

$i++;

}

}

echo $i.' Subscribers deleted';

} else {

echo 'No Subscribers deleted';

}

}

remove_subscribers();


Navjot Singh comments:

Try setting role => ''


Navjot Singh comments:

Ok. Don't do that. Code won't work.


Creativira comments:

I figured that. Thanks.


Navjot Singh comments:

Here is the working code


function remove_noroleusers() {
global $wpdb;
$args = array( 'meta_key' => 'wp_capabilities','meta_value' => 'a:0:{}','meta_compare' => '=') ;
$noroleusers = get_users( $args );
if( !empty($noroleusers) ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
$i = 0;
foreach( $noroleusers as $noroleuser ) {
if( wp_delete_user( $noroleuser->ID ) ) {
$i++;
}
}
echo $i.' Users deleted';
} else {
echo 'No Users deleted';
}
}
remove_noroleusers();


Just add once and go to the site.


Creativira comments:

Navjot, thanks for the efforts but zebra helped me with an sql code.

2013-09-22

Rowela Alzona answers:

Without using scripts there are 2 Ways to solve it.

1. THROUGH WP DASHBOARD
-Login to dashboard and click USERS > ALL USERS
-check all the users you want to remove and click apply > delete

2. THROUGH PHPMYADMIN
-Login to your CPANEL (i.e. http://www.website.com/cpanel)
-Go to your PHPMYADMIN
-Find the database field of your users called "wp_users" then hit "browse"
-Tick the "check all users" and leave the ones you want to maintain then hite delete..


Creativira comments:

Ellah, you did not read the requirements clearly. Thanks for playing though.

2013-09-22

Daniel Yoen answers:

Hello Vira,

you can try this, this code delete spam users daily

if(!wp_next_scheduled("node_clear_spam_user_daily"))
{
wp_schedule_event( time(), "daily", "node_clear_spam_user_daily");
}

function node_delete_spam_users()
{
require_once(ABSPATH . "wp-admin/includes/user.php");
global $wpdb;

$role = "subscriber"; // replace with your own

$this_role = "[[:<:]]" . $role . "[[:>:]]";

$query = $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' AND meta_value RLIKE %s", $this_role );

if ( $users_of_this_role = $wpdb->get_results( $query, ARRAY_N ) )
foreach ( $users_of_this_role as $user_id )
{
wp_delete_user($user_id[0]);
}
}
add_action("node_clear_spam_user_daily", "node_delete_spam_users");


but for the first time, put this code at the top of header.php file, then refresh your page and then remove this the code :

<?php node_delete_spam_users(); ?>

hope this help

Best,
Daniel


Daniel Yoen comments:

Hello Vira,

you can try this, this code delete spam users daily

if(!wp_next_scheduled("node_clear_spam_user_daily"))
{
wp_schedule_event( time(), "daily", "node_clear_spam_user_daily");
}

function node_delete_spam_users()
{
require_once(ABSPATH . "wp-admin/includes/user.php");
global $wpdb;

$role = "subscriber"; // replace with your own

$this_role = "[[:<:]]" . $role . "[[:>:]]";

$query = $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '{$wpdb->prefix}capabilities' AND meta_value RLIKE %s", $this_role );

if ( $users_of_this_role = $wpdb->get_results( $query, ARRAY_N ) )
foreach ( $users_of_this_role as $user_id )
{
wp_delete_user($user_id[0]);
}
}
add_action("node_clear_spam_user_daily", "node_delete_spam_users");


but for the first time, put this code at the top of header.php file, then refresh your page and then remove this the code :

<?php node_delete_spam_users(); ?>

hope this help

Best,
Daniel


Creativira comments:

Thanks Daniel, I've got it solved :)