A couple years ago I configured a site with Drupal 6.9, and then I forgot about it. I have 160 pages of users that are all spammers and I need to delete all but 3. Doing this one page at a time is painfully slow due to the MySQL calls (I guess). I also have about 2500 forum posts to delete.

I'm a little afraid to delete records from the database directly.

I saw a module called "mass delete", but it is for Drupal version 5 and not available for version 6.

9 Answers 9


You can use Views Bulk Operations, a module that allows to execute bulk operations on nodes, users, comments; it also permits to apply the selected operation to all the nodes, users, or comments using the Batch API. The batch API allows forms processing to be spread out over several page requests, thus ensuring that the processing does not get interrupted because of a PHP timeout, while allowing the user to receive feedback on the progress of the ongoing operations.

  • 1
    Selecting all items to delete is, again, painfully slow - There is a setting in the View's VBO options to enable a select all button that select all items across all pages - click it and Kill-em-all!
    – AKS
    Apr 26, 2013 at 18:21

This way to kill nodes is very slow, but most safe

function MYMODULE_menu(){
  $items['admin/mymodule/killnodes/%'] = array(
      'title' => 'Kill nodes',
      'page callback' => 'kill_nodes',
      'page arguments' => array(3),
      'type' => MENU_CALLBACK,
      'access arguments' => array('administer site configuration'),
  return $items;

function kill_nodes($type){
  $query = "SELECT node.nid AS nid FROM {node} node WHERE node.type IN ('%s')";
  $result = db_query($query, $type);
  $count = 0;
  while($row = db_fetch_object($result)){
  $message = t('!count nodes has been killed. Pif-Paf!', array('!count' => $count));
  return t("That's all folks");

For users, can you programmatically determine the user whom you want to remove? If it possible you can use previous function as example to remove users unwanded.

  • 3
    I would suggest wrapping that with a Batch API batch to prevent it from timing out.
    – Decipher
    Apr 3, 2011 at 2:30
  • For sure. It's a simple example.
    – dobeerman
    Apr 3, 2011 at 10:44

Instead of creating a custom module for this task, you can use a simple script and execute it with Drush. Since you need to process a lot of users and nodes, usage of the Batch API is recommended (and it can be used with Drush).


If, like me, you prefer a Python approach (rare around here probably, but still), this is a transparent and effective way to fix this problem:

import os

# Build up a variable containing the usernames
# This list was built using drush sql-cli, then
# SELECT name FROM users 
#   where $your-where-condition 
#   order by uid asc 
#   INTO 
#     OUTFILE '/tmp/users.csv' 
#     ENCLOSED BY '"' 
#     lines terminated by ', ' ;
users = [result from SQL goes here]

for user in users:
    print("Deleting spam user: %s..." % user),
    os.system('drush --yes -r $your-path-to-drupal -l $your-site-url user-cancel --delete-content %s > /dev/null' % user) 
    print 'Done'

The steps are basically:

  1. Log into your DB with drush sql-cli -r $your-path-to-drupal -l $your-site-url
  2. Run the SQL above with your own where condition and paste the results into the users variable.
  3. Update your drupal path and site name into the drush command
  4. Run the script with python delete-users.py

I'm sure there's a better way to do this, but this is my hacked solution that works well.

  • 1
    You can do this as a one-liner too. for i in `drush sql-query 'SELECT list of usernames'`; do drush --yes user-cancel --delete-content $i; done.
    – David L
    May 30, 2014 at 0:07

Try these 2 modules for D6:

User Prune https://drupal.org/project/user_prune and User Delete https://drupal.org/project/user_delete

User Prune lets you mass delete inactive users based on criteria you specify.

User Delete lets you delete a user and delete all submitted content including nodes and comments


Use advanced user module. This module adds an "advanced" tab to user management page. In that tab, you can filter users by any attribute (role, status etc.) and select all. If you select the deletion method of the users as delete the user and remove all content, you can also delete all content created by them.


If you have access to Drush and want a quick solution which lets you whitelist users to keep and doesn't involve installing additional modules:

drush @example.org sqlq "SELECT name FROM users WHERE name NOT IN ('keepthisuser1', 'andthisuser2')" | while read NAME; do
  drush -y @example.org user-cancel $NAME ; 

Make sure to adjust both the @example.org and list of users to keep ('keepthisuser1', 'andthisuser2') in the above before copy pasting :)

  • This will leave tails, like user fields and all.
    – niksmac
    Oct 28, 2015 at 4:47
  • 1
    Can you give details of what you mean? I just tested this on latest Drupal 7, and it did not leave data in user fields. I don't understand what you mean by "tails". Oct 28, 2015 at 7:06
  • 1
    I see this answer duplicates a similar one wrapped in Python above. Oct 28, 2015 at 7:10
  • 1
    I reversed my vote on this answer because it does in fact work as required. Recommend @niksmac change vote down to vote up. Note that it is not an SQL only answer and uses drush.
    – Christian
    Apr 16, 2020 at 7:55

The module Delete All could came in handy.

Once installed you can do, for example:

drush delete-all articles


drush delete-all users

Drupal 6 version in in dev but from the notes:

Everything should be working except for quick delete.


Original question is for 6, but using Chris Burgess' answer can be applied to Drupal 8 like so;

drush sqlq "SELECT name FROM users_field_data WHERE name NOT IN ('admin')" | while read NAME; 
  do drush -y user:cancel --delete-content $NAME; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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