Replace username with unique hash when users delete their own accounts
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-&gt;uid);
        $user_load-&gt;name = 'anonymous-'.time();// Here you can add your random number.
        user_save($user_load);
      }
    }