Score:2

How can I bulk delete users?

kr flag

My site got hacked and I had an online service clean it out. However, it now has over 150,000 user accounts, the bulk of which have no role assignment. How do I delete these accounts and their content?

PS: Drush solutions are appreciated and welcome, but for now I would prefer non-Drush answers.

I tried suggestion 1 at How to delete 'blocked' users in bulk? but both options are time consuming and the VBO module seemed to hang. Suggestion 2 seems to be the fastest option, but the /core/modules/user/user.admin.inc file doesn't exist in Drupal 8.7.

Score:4
cn flag

I just had to do this recently. I found the easiest thing was to build a list of the emails addresses to remove in a text file.

_remove_user_emails.txt

[email protected]
[email protected]
[email protected]

Then process them with the simple script below:

remove_users.sh

IFS="
"
pwd
for user in `cat _remove_user_emails.txt`
do
  echo "Cancelling user: $user"
  ./vendor/bin/drush -y user-cancel ${user}
done

You can derive your list however you'd like. I often use phpmyadmin and just write a query for what I need and export it. Something like this:

SELECT DISTINCT u.mail
FROM users u
LEFT JOIN users_roles ur on u.uid = ur.uid
WHERE u.uid > 1
AND u.status <> 0
AND ur.rid IS NULL
apaderno avatar
us flag
With over 150000 user accounts, it doesn't seem feasible to write the email addresses in a text file, and then delete the user accounts with Drush.
Mandock D. avatar
kr flag
@Ryan, I tried your suggestion on my test site, I had to adjust for execution time, etc, etc in my config files...something I don't have access to on the live site. Cancelled process after about 2hrs of running. Thanks, but I hope I can get a faster and more efficient solution.
cn flag
In order to remove users, you need to run all the hooks. I found drush to be the fastest way to do that. Perhaps instead of putting 150k users in a file, instead try a smaller set like 10k users. My site had about 50k users and we removed 30k over about 15 hours YMMV. Not sure why you had to edit the exec time, this is a bash script, that will run php over and over vs one long thread.
hotwebmatter avatar
nr flag
Why cancel the script execution after two hours? I'd recommend leaving it running overnight.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.