Last active
December 12, 2018 18:03
-
-
Save imonroe/9e3d598e4ffb52232846734bf71e9860 to your computer and use it in GitHub Desktop.
Drush - Delete a group of users in Drupal 7 from the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// We want to preserve all accounts from a selection of email domains. | |
// We want to preserve system accounts with UIDs 0 and 1 | |
// We want to delete all other accounts which are: | |
// - disabled, AND | |
// - have never been used, AND | |
// - have never been logged into | |
// | |
// We also want to use Drupal's user_delete_multiple() function so that | |
// all the necessary hooks are called throughout the system. | |
// | |
// Because this could be resource intensive, we break it up into chunks of | |
// 100 records at a time. | |
// | |
// So here's the un-concatenated PHP to accomplish this task: | |
$domains = [ | |
":1"=>"%legalshield%", | |
":2" => "%stateside%", | |
":3"=>"%legaldefence%", | |
":4"=>"%criticalmass%", | |
":5"=>"%thehangar%", | |
":6"=>"%whatarage%" | |
]; | |
$users = db_query("SELECT uid FROM {users} | |
WHERE (status=0 AND access=0 AND login=0) | |
AND (mail NOT LIKE :1 AND mail NOT LIKE :2 AND mail NOT LIKE :3 AND mail NOT LIKE :4 AND mail NOT LIKE :5 AND mail NOT LIKE :6) | |
AND (uid > 1)", $domains); | |
echo("Completed SQL.".PHP_EOL); | |
$uids = $users->fetchAllAssoc("uid"); | |
$u = array_keys($uids); | |
$chunks = array_chunk($u,100,true); | |
foreach($chunks as $c){ | |
echo("Processing Chunk.".PHP_EOL); | |
user_delete_multiple($c); | |
} | |
// However, we want to run this as a Drush command from the CLI. Here is a concatted version ready to run via Drush: | |
?> | |
drush php-eval '$domains = [":1"=>"%legalshield%",":2" => "%stateside%",":3"=>"%legaldefence%",":4"=>"%criticalmass%",":5"=>"%thehangar%",":6"=>"%whatarage%"]; $users = db_query("SELECT uid FROM {users} WHERE (status=0 AND access=0 AND login=0) AND (mail NOT LIKE :1 AND mail NOT LIKE :2 AND mail NOT LIKE :3 AND mail NOT LIKE :4 AND mail NOT LIKE :5 AND mail NOT LIKE :6) AND (uid > 1)", $domains); echo("Completed SQL.".PHP_EOL); $uids = $users->fetchAllAssoc("uid"); $u=array_keys($uids); $chunks = array_chunk($u,100,true); foreach($chunks as $c){ echo("Processing Chunk.".PHP_EOL); user_delete_multiple($c); }' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment