Is it possible to programmatically set all users to active?

This problem arises because I am trying to display my users using Views, but because my migrated users are "blocked", I cannot expose them to Views.

Could someone provide a snippet of code or a tutorial on how to set user status to active?

This is the code that I need to execute at the beginning.

define('DRUPAL_ROOT', getcwd()); 
$_SERVER['REMOTE_ADDR'] = "localhost"; 
require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; 

// Set the active DB 
  • Which "approve process" do you mean? AFAIK, approving a user is nothing more than setting the user status to 1. Can you be more specific about what else you expect to happen (maybe sending an e-mail to the admin?), or which hooks you expect to be invoked? Commented Apr 5, 2012 at 8:05
  • Drupal has already a page where the administrator users can approve the registration done from new users. Why do you need a custom module for this?
    – espero
    Commented May 5, 2012 at 20:15

5 Answers 5


I would use the following code.

$query = new EntityFieldQuery();

$result = $query->entityCondition('entity_type', 'user')
  ->propertyCondition('status', 0)
  // Avoid loading the anonymous user.
  ->propertyCondition('uid', 0, '<>')
  // Comment out the next line if you need to enable also the super user.
  ->propertyCondition('uid', 1, '<>')

if (isset($result['user'])) {
  // Disable the email sent when the user account is enabled.
  // Use this code if you don't use the code marked with (1).
  // $old_value = variable_get('user_mail_status_activated_notify', TRUE);
  // variable_set('user_mail_status_activated_notify', FALSE);

  $uids = array_keys($result['user']);
  $users = entity_load('user', $uids);

  foreach ($users as $uid => $user) {
    $user->status = 1;
    $original = clone $user;      // (1)
    $user->original = $original;  // (1)
  // Restore the value of the Drupal variable.
  // Use this code if you don't use the code marked with (1).
  // variable_set('user_mail_status_activated_notify', $old_value);
  • The code loads only the accounts that are not enabled. It is useless to load the accounts already enabled.
  • The code avoids loading the anonymous user account, that is not a real account.
  • Clive is right when he says that using user_save() Drupal could send an email to the enabled users. The code used from the function is the following one.

    // Send emails after we have the new user object.
    if ($account->status != $account->original->status) {
      // The user's status is changing; conditionally send notification email.
      $op = $account->status == 1 ? 'status_activated' : 'status_blocked';
      _user_mail_notify($op, $account);

    With my code, the condition $account->status != $account->original->status is not verified, and the email is not sent. As alternative, you could set the value of the Drupal variable "user_mail_status_activated_notify" to FALSE before calling user_save(), as shown in the code. Changing the value of that Drupal variable would have a global effect, and it would not work when other code changes its value to TRUE. Setting $user->original to a copy of the $user object is the only way to be sure the call to user_save() doesn't effectively send any email to the users, for the user objects being saved with my code.

  • @kiamlaluno, I used your code, but I am also getting errors. I've updated my post, would you have any ideas on what could be wrong?
    – chlong
    Commented May 31, 2012 at 17:21
  • @chlong If I remember rightly your sage database is a non Drupal one isn't it? If so, remove the call to db_set_active() before you run the code
    – Clive
    Commented May 31, 2012 at 18:09
  • @chlong I don't get any exception, when using my code. Be sure the database you are using (the one set with db_set_active()) contains the tables installed from Drupal.
    – apaderno
    Commented May 31, 2012 at 18:09
  • @kiamlaluno, my 'sage' database is a drupal database, it contains all of the drupal tables. But I will try running without 'db_set_active()' - EDIT: without 'db_set_active()' the code will run fine, but the users in my 'sage' database are not modified. - EDIT2: Your code does indeed work, but it only changed the statuses of the users in my default site, which isn't what I want :(
    – chlong
    Commented May 31, 2012 at 18:14
  • @chlong If "sage" is a Drupal database associate with a Drupal site, then try executing the code from that site; if you get the same error, then the database effectively doesn't contain all the necessary tables. As far as I know, using db_set_active() doesn't cause any query to fail.
    – apaderno
    Commented May 31, 2012 at 18:48

You can use a combination of user_load_multiple() and user_save() to update the status property of the users:

$uids = db_query('SELECT uid FROM {users}')->fetchCol();
foreach (user_load_multiple($uids) as $account) {
  $account->status = 1;

It's worth bearing in mind that any mail notifications you have set to inform users of their account status will be invoked using this method.

If you don't want that to happen I think you'll have to go directly into the {users} table and set the status manually (not recommended):

  ->fields(array('status' => 1))

You have to rely on user_load to load the $user object, edit what's appropriate, in your case $user->status, then user_save the modified object.

You could also use db_query to update the users table directly if you know what you're doing.

As for triggering the rule, this post explains how to do it.


This way you can set the status of all user to active.

$users = entity_load('user');
foreach ($users as $user) {
  if ($user->uid != 0) {
    $user->status = 1;
    entity_save('user', $user);

entity_save() is implemented from the Entity API module.


To expand on Countzero's answer a bit with an example:

If we have a function in a custom module that is passed the UID, for this example we will update a user from 'blocked' to 'active' using Drupal's user_load() and user_save() functions.

function MYMODULE_update_user($uid) {

// Load user object.
// To avoid confusion and to avoid clobbering the global $user object,
// assign the result of this function to a different local variable.
$account = user_load($uid);

// Change user from 'blocked' to 'active'.
user_save($account, array('status'=> 1));

The user_save() documentation is as follows:

user_save($account, $edit = array(), $category = 'account')


$account: (optional) The user object to modify or add. If you want to modify an existing user account, you will need to ensure that (a) $account is an object, and (b) you have set $account->uid to the numeric user ID of the user account you wish to modify. If you want to create a new user account, you can set $account->is_new to TRUE or omit the $account->uid field.

$edit: An array of fields and values to save. For example array('name' => 'My name'). Key / value pairs added to the $edit['data'] will be serialized and saved in the {users.data} column.

$category: (optional) The category for storing profile information in.

Return value

A fully-loaded $user object upon successful save or FALSE if the save failed.

To determine what part of the $edit array() to modify, you can do something like a hook_form_alter to dpm() out the user object.

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.