4

I have been asked to help look after a Drupal site that was created by a third party. This site has lots of users (~13,000) and for each user lots of items of content was created.

I want to clean the database from users and content to improve performance. When a user try to log into the site via an ID a user account is created, a web service is called and the correct content is recreated. My next job is to create some cron tasks to keep the data controllable going forward.

I want a way to delete all the users apart from user id 1. I looked at VBO but every now and again it will stop running and I have to start again (it has never completed).

I have created a drush command which calls the following:

function delete_users_batch_delete() {
  drush_print('Deleting Users');
  $uids = delete_users_get_uids();
  $count = count($uids);

  $temp = 0;
  foreach ($uids as $uid) {
    $temp++;
    user_delete($uid);
    if ($temp % 25 == 0) {
      drush_print('deleted ' . $temp . ' from ' . $count);
    }
  }
  drush_print('All users deleted');
}

function delete_users_get_uids() {
  $result = db_query('SELECT u.uid FROM {users} u WHERE u.uid > 1');
  $uids = $result->fetchCol();
  return $uids;
}

However this is running very slow on my local machine. What would be the best way to delete everything quickly.

If I had started the project myself I would just run my install script which would set up a new instance of the site with it configured correctly. Sadly I don't have this and just one big database dump.

3
  • If site is so user and data-centric, is it wise to simply delete all that?! I mean, if my 13000 users are complaining site is slow, I would be terrified to think what they would say if I would suddenly kick them out of it and delete their content. Site would be fast, but with 13000 people less to earn on. And if one user generates mere $1 / month, it's pretty amazing difference in one's wallet. Are you damn sure site owner wants this? Do you have it written and signed?
    – Mołot
    Mar 27 '14 at 13:51
  • 1
    @Mołot all the content is stored in another backend system. Logging in with a guid calls a web service that gets the data for that user. A drupal user is created and the content from the WebService is used to generate content in Drupal. There was a bug when the project launched which meant that each time someone logged in new content was created rather than using the referenced content. It is a big mess. If was starting from scratch I would do it very different!
    – Johnny
    Mar 27 '14 at 14:00
  • Well, that explains a lot. Now, feel free to comment on my answer if it causes you any problems.
    – Mołot
    Mar 27 '14 at 14:01
5

You can use Views Bulk Operations (VBO) module...

Above module 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.

There is a setting in the View's VBO options to enable a select all button that select all items across all users and unselect admin.

2
2

You can do this with Devel's Devel Generate submodule. Go to Generate users, set the number of users to generate to 0 and tick the Delete all users (except user id 1) before generating new users option, then click on the Generate button.

1

There is user_delete_multiple(array $uids) function just for mass deletion. So do not loop on your own, let the Drupal do it.

Delete multiple user accounts.

Parameters

  • $uids: An array of user IDs.

Note that it might not be much faster as it still needs to loop for hooks invocation.

Also, array with 13000 entries may be a bit big for ->condition('uid', $uids, 'IN') call, I would suggest to use some kind of batch processing and split whole list to chunks somewhere between 100 and 1000 users per chunk, depending on your configuration (calculations are hard, easier to test).

1

With the devel_generate module which is a submodule of the great Devel module you could also use Drush to do that.

Please backup your DB in advance ;)

Delete ALL content.

drush genc --kill 0 0

Delete all articles.

drush genc --kill --types=article 0 0

Delete all users except uid 1.

drush genu --kill 0

For genu with --kill that's a hard coded condition in the query: ->condition('uid', 1, '>') to not delete uid 1.

2
  • How do I install devel_generate? My drush does not undestant genu 0.
    – jgrocha
    Jul 5 '18 at 15:33
  • 1
    @jgrocha – drush en devel and then drush en devel_generate and then whatever else you would like to run from this answer.
    – leymannx
    Jul 5 '18 at 16:05

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.