How can I delete all blocked users with Drush? I have hundreds of blocked users from spammers. I want to delete all these users. I tried doing it through the web interface, but this only works for about 20 users at a time. I have 60 pages to delete, which takes too long and is unsustainable. Also, I want all the relevant callbacks to be run when a blocked user is deleted, and I want all its content deleted. I noticed user-cancel
, but this appears to only apply to a single user. Is there a way to make this command work with all blocked users?
-
Here is your response :). Custom module for drupal 7. In the admin/people area there is a new form for complete this operation. Too with drush. http://drupal.stackexchange.com/questions/38942/how-to-delete-blocked-users-in-bulk/85300#85300 – lgrtm Sep 11 '13 at 07:26
3 Answers
Using Drupal 7 and Drush 5.9, the way I did was:
1- Create an alias "@example-local" to your local Drupal installation, as shown here.
2- Create a script file called "delete-blocked-users.script", at, say, "/Users/username/scripts/drush", with the following code:
#!/usr/bin/env drush
$users = db_query("SELECT * FROM {users} WHERE (status = 0)");
foreach ($users as $user) {
exec("drush @example-local user-cancel $user->name -y");
}
3- Make the script file executable with:
chmod +x ~/scripts/drush/delete-blocked-users.script
4- And finally, run the script using:
drush @example-local scr ~/scripts/drush/delete-blocked-users.script
This will delete all the blocked users (users with status = 0), without asking for confirmation (defined by the -y flag).

- 6,980
- 1
- 38
- 48
Drush does not have a simple command to delete all blocked accounts. It would probably be easier to set up a vbo view in you admin interface that lets you filter for blocked users then you can select all and cancel.
You can also run a php script with drush php-script script.php
.
So you can set up a php script that does a query for blocked users then loops through though users and calls the user cancel function.

- 3,121
- 3
- 28
- 43
Besides drush, is just easier access to mysql data using user, password and database name that you can find in file drupalhomedir/sites/default/setting.php with command:
mysql -u username-at-setting -ppassword-at-setting database-at-setting
then delete all records on drupal 'user' table with:
DELETE FROM user WHERE status=0;

- 13
- 3
-
2While technically correct, this will delete records from the users table but would orphan a lot of data that is attached to a user in Drupal. Custom fields and contributed content would all be left behind. – zkent Jul 29 '14 at 09:19