10

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.

17

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! – AyeshK Apr 26 '13 at 18:21
3

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)){
    node_delete($row->nid);
    $count++;
  }
  $message = t('!count nodes has been killed. Pif-Paf!', array('!count' => $count));
  drupal_set_message($message);
  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 '11 at 2:30
  • For sure. It's a simple example. – dobeerman Apr 3 '11 at 10:44
3

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).

2

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' 
#     FIELDS TERMINATED BY ',' 
#     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 '14 at 0:07
1

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

1

I have implemented a custom module for delete all users indrupal 7. In the admin/people area there is a new form for complete this operation.

Too with drush.

Is a sandbox project. Thk.

Link to project.

1

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.

0

The module Delete All could came in handy.

Once installed you can do, for example:

drush delete-all articles

or

drush delete-all users

Drupal 6 version in in dev but from the notes:

Everything should be working except for quick delete.

-1

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 ; 
done

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 '15 at 4:47
  • 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". – Chris Burgess Oct 28 '15 at 7:06
  • I see this answer duplicates a similar one wrapped in Python above. – Chris Burgess Oct 28 '15 at 7:10

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.