I'm new to Drupal development and am way over my head on this one. My company runs a subscription-based site offering market research to corporations. A company pays for a subscription and is granted a certain number of users. The person designated as primary user has the ability to add the remaining users themselves. We now need to add a function to allow them to delete a user who is no longer with their company. In doing so, we need the following to happen:

  1. Delete the record from "users" database table
  2. Change the author on any node created by the deleted user to "Anonymous"
  3. Send an email to our internal support address letting them know that the user should be removed from other non-connected systems

The previous developer created a custom module for account management that allows adding users under several different circumstances. For example the user is completely new to the company adding it, which means they need to be added to our other unconnected systems as well, as opposed to providing a Drupal account to a user who is already in our other systems.
Currently a lot of stuff is being done with JavaScript using Drupal.parseJson to process the request based on the segments of the URL clicked. This was done for the sole reason of being able to update the page with the user list without a complete refresh. I personally don't care if this new function works that way, or if it needs to hit the server and refresh the page. I just need to find a way to do everything in the above list with a single click.

I've looked at the API and found user_delete, but it requires two params - $edit and $uid. The uid is no problem, but all I can find about $edit is that it's "an array of submitted form values." We're not using a form, though; our user lists are static text, and the actions all occur onclick of an href (at this point); so there's nothing being submitted. Is there any way I can fake the values of $edit? Or is there another way to do this? I'm reading about hook_user() and thinking that I'd call user_delete() somewhere to do the actual delete, and then my hook_user() implementation would handle the rest with the $op = delete param. But before I worry about how to get the other stuff happening, I need some guidance about how to call user_delete().

Is it even worth it to use user_delete()? Would it be less trouble to just write my own queries to delete the user record and change any content records to "Anonymous"?


3 Answers 3


This is not that hard, for D6 it would look like this:

  1. Delete record from Drupal database users table;

    user_delete(array(), $uid);
  2. Change author on any content created by deleted user to Anonymous;

    handled by the node module.

  3. Send an email to our internal support address letting them know that the user should be removed from other non-connected systems


    See hook_mail for details.

  • 1
    The Node module in core will already set the posts to Anonymous, so that part you can skip.
    – Chaulky
    Jul 21, 2011 at 19:53
  • @googletorp, so I don't actually need to pass any values in for the first param of user_delete()? That's what I was so hung up on and couldn't find any documentation for.
    – EmmyS
    Jul 21, 2011 at 20:11
  • @Chaulky - so just calling user_delete will force the Node module to set the posts to Anon.? I don't have to do anything else?
    – EmmyS
    Jul 21, 2011 at 20:12
  • @EmmyS no you don't need to pass any value - an empty array will do. The user module use it internally when forms are submitted.
    – googletorp
    Jul 21, 2011 at 20:47

Is it even worth it to use user_delete()? Would it be less trouble to just write my own queries to delete the user record and change any content records to "Anonymous"?

To delete a user account, and assign all the nodes the user authored to the anonymous user, you simply need to call user_delete(array(), $account->uid), or user_delete(array(), $uid), depending if you have an user object, or a user ID.

As for assigning the nodes created with the user account being deleted, user_delete() invokes all the implementations of hook_user() passing "delete" as value for $op. The Node module implements the hook using the following code:

function node_user($op, &$edit, &$user) {
  if ($op == 'delete') {
    db_query('UPDATE {node} SET uid = 0 WHERE uid = %d', $user->uid);
    db_query('UPDATE {node_revisions} SET uid = 0 WHERE uid = %d', $user->uid);

As you see, the function already updates the nodes authored by the user account being deleted, and any node revisions where associated with that user account.

The first parameter required from user_delete() is normally not used from the implementations of hook_user(), when the $op parameter is set to "delete." In most of the cases, the modules just use $account->uid to identify the user data they wrote in their own database tables. Even supposing the modules saved the data using another value to identify the user account, it is a value contained in the user object.

As per writing your own queries to delete the user account, I would not suggest doing it, as there could be modules that need to be informed when a user account is delete, usually to delete the data the module associated to that account. Rewriting code when there is already a function for this specific purpose could mean also to update the code when passing to a new Drupal version. The code would not be as generic as the code already used by Drupal.


Non-Coding method:

Modules needed: Triggers (part of core)

Step 1: Enable trigger module

Step 2: Create the send email action

  • goto admin/settings/actions
  • create 'Send email...' action
  • fill out the form

create send email... action

Step 3: Create the trigger

  • goto admin/build/trigger/user
  • under Trigger: After a user has been deleted choose the action you just created

create trigger

And your done! By default, when you delete a user, all of their post turns to anonymous.


You now have a choice of where you want to delete users now.

  • You can bulk delete users by going to /admin/user/user
  • Go to the user profile page and delete
  • Create your own custom module and function that calls user_delete(array(), $uid)

Everyone of these ways will automatically sent an email out for every deleted user. I believe this way is more flexible as you can change the email body and recipient without editing any code.

  • This doesn't actually delete the user though and the OP isn't using the typical "edit user" form to delete it.
    – Chaulky
    Jul 21, 2011 at 19:53
  • I picture them getting a list of users to delete, going to /admin/user/user and bulk deleting them
    – iStryker
    Jul 21, 2011 at 20:10
  • @iStryker, Chaulky is right - we are not doing it that way. This is custom all the way, not accessing user/ at all.
    – EmmyS
    Jul 21, 2011 at 20:15
  • Actually now that I think of it, calling user_delete() should email users with this trigger. Also just realized if you delete 10 people, it will send 10 emails. Good luck with your custom coding.
    – iStryker
    Jul 21, 2011 at 20:21
  • Yes, we're aware of the individual emails; that's what we want. And I inherited this; it wasn't my choice to deviate from default functionality.
    – EmmyS
    Jul 21, 2011 at 21:27

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.