I am writing a script to delete 200+ users. To achieve this, I write a db_select query and got all the UIDs. The I wrote foreach loop to delete users using 'user_delete' function. But this is taking lot of time to delete a single user. It will be very hard to delete 200+ users.

Can someone help me to solve this program to delete bulk users programmatically?


If you want to maintain data integrity, there's not much you can do to speed the process up except upgrade your server's hardware.

user_delete calls user_delete_multiple, and it has plenty to do for each record; not just deleting the user record, it deletes fields/data, invokes hooks, clears caches, etc. All of that needs to happen or you're likely to find problems further down the line.

If you're finding that the slowdown is causing the script to timeout, you can process the list with the Batch API and solve that at least.

Incidentally, if you don't care about data integrity, you can just cherry pick the lines from user_delete_multiple that you do need, and execute those directly instead.

  • 1
    drush scr is also an option instead of Batch API. I used it all the time for little tasks like this.
    – mpdonadio
    Jan 10 '18 at 14:24

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.