Is there a way I could run mysql query from the backend to delete all the users and its related records except master-login.

There are some modules that help to do it or a via drush command but I am trying to do this from the backend database.

I am not sure about the how all the tables are maps. instead of figure it out I am trying to get a quick help here.



You can install the Delete All module which provides some command line Drush commands to remove all content and users safely.

This module is used to delete all content and/or users from a site. This is mainly a developer tool, which can come in handy in several cases, listed below.

The usual way to do this is to go to Administer -> Content then select all the nodes and delete them. This works if you have a handful of nodes only. If you have hundreds or thousands of nodes, then it is not a practical solution.

Another option is to directly delete the nodes from the node table in the database. This does not work properly, since there are also comments, and many tables for add on modules that needs to be cleaned.

  • This is a test site that the client was using for a period of time, and they must clean it up before starting with real data.
  • You are testing something that creates a lot of nodes (e.g. aggregator), and want to do it over and over again.
  • You created a site in the past and want to replicate it again, but with new content.

Note that for nodes, comments and all additions to nodes that contributed modules may have added. For users, any additional module data will also be deleted.


Use the following script to delete all users.

$table_select = db_select('users', 'u')
              ->condition('name','master_login_name','!=') // Put admin name here in place of 'master_login_name'

foreach($table_select as $users)

user_delete function will delete users from drupal tables.user_delete

  • above query will delete only user but how to delete user related data i.e, node etc... – Fawwad Jul 17 '14 at 5:18
  • if you want to delete only node part then firstly you have to get nodes id according to users id. and use this function node_delete($nid). – TBI Infotech Jul 17 '14 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.