How can i delete all users and only keep the current admin user?
thanks!

Comments

lavamind’s picture

Using Views and Views Bulk Operations (VBO), you could create a view that returns all the users you want to delete (ie. with a filter such as uid != 1). VBO allows you to configure your view to run operations such as delete on the result set.

bjlewis2’s picture

Install Devel and enable the Devel Generate sub-module.

Generate 0 users and delete all existing users (except user1).

Here's a screenshot to demonstrate: http://screencast.com/t/yr15syPcwBE

rbordallo’s picture

Great bjlewis2! Another way is using the module delete_all: https://www.drupal.org/project/delete_all

bjlewis2’s picture

Looks like a good module to use if you don't already have devel installed. If you do, then I wouldn't install another module since devel does all of that. However if you don't have devel, I expect delete_all has a significantly smaller footprint, and might be better in that sense.

lawsonmediapub’s picture

I have used this module before but now I forgot how to use it. I see no instructions on the module page... Any help please? I need a tutorial on how to use the Delete All module. How do I get to the screen that allows me to perform the bulk operation to delete all users with this module?

freeofficesolutions’s picture

Worked like a treat once Devel Generate was enabled.

Navigate to Home » Administer » Generate items » Generate users then check the box Delete all users (except user id 1) before generating new users. as Brian suggested.

smndey’s picture

This is what I was looking for. Thanks Guys!

sachinsuryavanshi’s picture

thats awesome!

bowen111’s picture

I've been looking into this, but was having trouble as I'm working with a large number of users (in the thousands) and all above methods were timing out/throwing errors.

I decided to approach it with a Bash script utilising Drush:

NOTE: This method only works for usernames with no spaces, the sed command in the for loop will need adjusting otherwise.

First I needed to get a list of all non-admin Drupal users, the following SQL achieved this, using "mysql -s -r" to get a clean list in terminal.

SELECT DISTINCT DR_USERNAME_FIELD FROM DR_USER_TABLENAME as u WHERE DR_USERNAME_FIELD NOT IN (SELECT DISTINCT DR_USERNAME_FIELD FROM DR_USER_TABLENAME as u JOIN DR_USER_ROLE_TABLE as ur ON ur.uid = u.uid WHERE ur.rid = 3);

I formatted these into a comma-separated list and placed this into a Bash file called usernames.sh:

users="COMMA_SEPARATED_USERNAMES"

For the main logic, I created another Bash file (making sure they're both in the same directory) called delete_users.sh. Looping over the users filtered by sed, and running Drush ucan to cancel the user.

source usernames.sh

for i in $(echo $users | sed "s/,/ /g")
do
    cd DRUPAL_ROOT_PATH
    drush -y --quiet --user=1 ucan $i
    echo "user $i cancelled"
done

Finally, in the scripts directory, the command ./delete_users.sh will run the script. Make sure the scripts have correct execute permissions.

This was it for me as I have the default user cancel option set to delete user. If this isn't the case for you, running a much simpler SQL DELETE query is now possible as user_cancel module hooks have been called, targeting all users with status = 0 (assuming no existing cancelled users exist).

Hope this helps someone in the future!

tunprog’s picture

You can use this module to delete all users but admin:

https://www.drupal.org/project/delete_all

norman.lol’s picture

drush entity:delete user deletes all users except user 1.

https://github.com/drush-ops/drush/blob/11.x/src/Drupal/Commands/core/En...