Skip to content

Instantly share code, notes, and snippets.

@aaronott
Last active December 12, 2015 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronott/4760022 to your computer and use it in GitHub Desktop.
Save aaronott/4760022 to your computer and use it in GitHub Desktop.
Disable all Drupal users except Admin via Drush
#!/usr/bin/env drush
<?php
#
# This script removes all drupal users with uid > 1 (e.g., skips
# admin user) and reassigns their content to anonymous.
#
// check if we can bootstrap
$self = drush_sitealias_get_record('@self');
if (empty($self)) {
drush_die("I can't bootstrap from the current location.", 0);
}
// we don't want to say yes each time.
drush_set_context('DRUSH_AFFIRMATIVE', TRUE);
$result = db_query("SELECT name from {users} WHERE uid > 1");
foreach ($result as $user) {
drush_invoke('user-block', array($user->name));
}
@glopes
Copy link

glopes commented Feb 22, 2013

hi aaronott,

i was thinking how i could make something similar, when i found this. :)
thx for that.
i tried to delete all users except uid 0 and uid 1 (uid > 1) with user-cancel. but that wasn´t working for me. using drush_invoke('user-cancel', array($user->name)); deletes the user with uid 0 and for now on drush throws this error message: "Could not login with user ID #0 ..."
any idea whats runnig wrong?

@aaronott
Copy link
Author

aaronott commented Sep 5, 2013

glopes, sorry for the delay. by now you've no doubt found your solution, but if not you can take a look at one of the other scripts I wrote here: https://gist.github.com/aaronott/6040509 and perhaps that may help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment