最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Bulk delete users by role? SQL?

programmeradmin1浏览0评论

I need to bulk-delete a large number (4,321) of users, all of a particular role type (not an out-of-the-box role name).

I have tried several functions found online, and have tried the Bulk Delete (WPBulk) plugin, but both throw a 500 server error. (I recently extended by server's max value to allow for more processing). I suspect changing the number of users visible on the Users listing might break things.

I suspect the best answer is to use SQL inside phpMyAdmin. I would need some hand-holding (ie. enter a query in to the "SQL" box in the UI?). The users have many custom fields inside wp_usermeta.

Thank-you.

I need to bulk-delete a large number (4,321) of users, all of a particular role type (not an out-of-the-box role name).

I have tried several functions found online, and have tried the Bulk Delete (WPBulk) plugin, but both throw a 500 server error. (I recently extended by server's max value to allow for more processing). I suspect changing the number of users visible on the Users listing might break things.

I suspect the best answer is to use SQL inside phpMyAdmin. I would need some hand-holding (ie. enter a query in to the "SQL" box in the UI?). The users have many custom fields inside wp_usermeta.

Thank-you.

Share Improve this question asked Mar 11, 2016 at 14:09 Robert AndrewsRobert Andrews 9881 gold badge19 silver badges42 bronze badges 1
  • "...all of a particular role type." What is the role type? Add it to your question. – markratledge Commented Mar 11, 2016 at 16:09
Add a comment  | 

1 Answer 1

Reset to default 1

This can be done efficiently with a SQL query, but it won't be so easy to set it up. It would have to delete the users, its meta data, to reassign their associated posts, etcetera.

As it is an operation that will run only once and will not have any long-lasting performance impact, I think it can be achieved using some WordPress core functions in a very easy way:

<?php

    /*
        Plugin Name: Delete users by role
        Description: Delete all the users with a hardcoded specific role
        Version: 0.1
        Author: Your name
        Author URI: http://www.yoursite/
    */

    function wpse220426_delete_users_by_role() {

        $args = array(
            'role'         => 'user_defined_role' // Modify to match your needs
        );

        $users_to_delete = get_users( $args );

        foreach( $users_to_delete as $user_to_delete ) :

            // wp_delete_user() accepts another user ID as a second parameter
            // in case you want to reassign the content to an active user
            wp_delete_user( $user_to_delete->ID );

        endforeach;

    }
    add_action( 'admin_init', 'wpse220426_delete_users_by_role' );

?>

Remember to backup your database before as it will run straight after activation without the need to trigger any button. Once it has finished deleting the users, also remeber to deactivate o delete it.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论