Error message

You are browsing documentation for drupal 7.x, which is not supported anymore. Read the updated version of this page for drupal 11.x (the latest version).

function user_cancel

Same name and namespace in other branches
  1. 11.x core/modules/user/user.module \user_cancel()
  2. 10 core/modules/user/user.module \user_cancel()
  3. 9 core/modules/user/user.module \user_cancel()
  4. 8.9.x core/modules/user/user.module \user_cancel()

Cancel a user account.

Since the user cancellation process needs to be run in a batch, either Form API will invoke it, or batch_process() needs to be invoked after calling this function and should define the path to redirect to.

Parameters

$edit: An array of submitted form values.

$uid: The user ID of the user account to cancel.

$method: The account cancellation method to use.

See also

_user_cancel()

3 calls to user_cancel()
user_cancel_confirm in modules/user/user.pages.inc
Menu callback; Cancel a user account via e-mail confirmation link.
user_cancel_confirm_form_submit in modules/user/user.pages.inc
Submit handler for the account cancellation confirm form.
user_multiple_cancel_confirm_submit in modules/user/user.module
Submit handler for mass-account cancellation form.

File

modules/user/user.module, line 2499

Code

function user_cancel ($edit, $uid, $method) {
 global $user;
 $account = user_load ($uid);
 if (!$account) {
 drupal_set_message (t ('The user account %id does not exist.', array(
 '%id' => $uid,
 )), 'error');
 watchdog ('user', 'Attempted to cancel non-existing user account: %id.', array(
 '%id' => $uid,
 ), WATCHDOG_ERROR );
 return;
 }
 // Initialize batch (to set title).
 $batch = array(
 'title' => t ('Cancelling account'),
 'operations' => array(),
 );
 batch_set ($batch);
 // Modules use hook_user_delete() to respond to deletion.
 if ($method != 'user_cancel_delete') {
 // Allow modules to add further sets to this batch.
 module_invoke_all ('user_cancel', $edit, $account, $method);
 }
 // Finish the batch and actually cancel the account.
 $batch = array(
 'title' => t ('Cancelling user account'),
 'operations' => array(
 array(
 '_user_cancel',
 array(
 $edit,
 $account,
 $method,
 ),
 ),
 ),
 );
 // After cancelling account, ensure that user is logged out.
 if ($account->uid == $user->uid) {
 // Batch API stores data in the session, so use the finished operation to
 // manipulate the current user's session id.
 $batch['finished'] = '_user_cancel_session_regenerate';
 }
 batch_set ($batch);
 // Batch processing is either handled via Form API or has to be invoked
 // manually.
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.