When users delete their own accounts (with the core function), I don't want the username to be replaced by anonymous, but with a hash string, for example anonymous-dhjf4ued.

I tried doing it with the Rules module, and I looked for modules that could do it, but I couldn't figure it out.

The reason this would be useful is because I have sites with a forum and threads are much more readable if you can still identify which users are the same (who responds to who) if there are two cancelled user accounts in a single thread.


1 Answer 1


You can use hook_user_cancel() like below:

   * Implements hook_user_cancel()
    function example_user_cancel($edit, $account, $method) {
      // Define in which method you are canceling the account, else remove it and 
      // proceed with below code by removing this if condition.
      if ($method == 'user_cancel_block') {
        $user_load = user_load($account->uid);
        $user_load->name = 'anonymous-'.time();// Here you can add your random number.
  • There is no need to load the account, since it is already passed to the hook. Also, the hook works just in a case; in the other cases, it doesn't do anything. So, having a module implementing that hook is not sufficient to achieve what the OP wants.
    – apaderno
    May 2, 2018 at 13:40
  • Also the call to user_save() is not necessary, since that function is called from Drupal after invoking that hook.
    – apaderno
    May 2, 2018 at 13:43

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.