Owning a blog with public registration is a huge mess, especially with all those nasty spammers. It's really hard to get rid of all those nasty spammers. In future, I'll share a very effective way to get rid of all future spam users, but first it's important to delete existing spam users. For that we have to delete all public registered accounts.
You can also use this tutorials for any other thing, since it's always not about spammers. Instead of deleting users of a specific role one-by-one, you can bulk delete them. As always, "Bulk Is Better" - Hardeep Asrani (Me)
As I know, there are five different roles in WordPress, which are admins, developers, authors, editors, contributors and subscribers. You can easily delete users of any of these roles easily by running a very simple code.
How To Bulk Delete All Users Based On Their Role:
- Go to your WP Dashboard > Appearance > Editor
- Open the functions.php file from the right sidebar.
- Add the following lines of code (based on user role) to your functions.php file and publish.
Codes For User Roles:
For Admin:
function remove_administrators() {
global $wpdb;
$args = array( 'role' => 'Administrator' );
$administrators = get_users( $args );
if( !empty($administrators) ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
$i = 0;
foreach( $administrators as $administrator ) {
if( wp_delete_user( $administrator->ID ) ) {
$i++;
}
}
echo $i.' Administrators deleted';
} else {
echo 'No Administrators deleted';
}
}
remove_administrators();
For Developers:
function remove_developers() {
global $wpdb;
$args = array( 'role' => 'Developer' );
$developers = get_users( $args );
if( !empty($developers) ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
$i = 0;
foreach( $developers as $developer ) {
if( wp_delete_user( $developer->ID ) ) {
$i++;
}
}
echo $i.' Developers deleted';
} else {
echo 'No Developers deleted';
}
}
remove_developers();
For Authors:
function remove_authors() {
global $wpdb;
$args = array( 'role' => 'Author' );
$authors = get_users( $args );
if( !empty($authors) ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
$i = 0;
foreach( $authors as $author ) {
if( wp_delete_user( $author->ID ) ) {
$i++;
}
}
echo $i.' Authors deleted';
} else {
echo 'No Authors deleted';
}
}
remove_authors();
For Editors:
function remove_editors() {
global $wpdb;
$args = array( 'role' => 'Editor' );
$editors = get_users( $args );
if( !empty($editors) ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
$i = 0;
foreach( $editors as $editor ) {
if( wp_delete_user( $editor->ID ) ) {
$i++;
}
}
echo $i.' Editors deleted';
} else {
echo 'No Editors deleted';
}
}
remove_editors();
For Contributors:
function remove_contributors() {
global $wpdb;
$args = array( 'role' => 'Contributor' );
$contributors = get_users( $args );
if( !empty($contributors) ) {
require_once( ABSPATH.'wp-admin/includes/user.php' );
$i = 0;
foreach( $contributors as $contributor ) {
if( wp_delete_user( $contributor->ID ) ) {
$i++;
}
}
echo $i.' Contributors deleted';
} else {
echo 'No Contributors deleted';
}
}
remove_contributors();
For Subscribers:
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();
For None:
There is also a role in WordPress called "none." Here is the code to bulk delete users with "none" rule. I'm not 100% about this code, so please leave a comment if it's working:
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();
Final Step:
Once you have went to your site go back to your functions file and remove the code. Don't forget to leave a comment.