Restore the anonymous (user ID 0) user record

Last updated on
28 June 2025

Having the Anonymous user deleted in your site breaks Drupal in unexpected ways, including a message like:

  • "Could not login with user ID #0." error from Drush
  • "warning: Invalid argument supplied for foreach() in /includes/form.inc on line 1181." and various other strange errors from Drupal core

This documentation covers two situations

  1. An import of a database caused the problem.
  2. It is not known how the anonymous user was deleted.

A database import changed the uid values

Certain database dump programs, such as older versions of phpMyAdmin, do not respect MySQL's SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; directive. The result of this is when importing a faulty database dump, your anonymous user record gets imported as (highest user ID) +1 instead of user ID 0.

Should this happen to you, you can fix your database by issuing the following query for Drupal 7:

UPDATE users SET uid = 0 WHERE name = '';

To check if this fix applies to you run:

select name, uid
from users
where name = '';

Output should be something like this:

-+------+-----+
-| name | uid |
-+------+-----+
-|      |  0  |
-+------+-----+

Not known how the anonymous user was deleted

Drupal 10+

For Drupal 10+, you can run the following queries:

INSERT INTO users (uid, langcode, uuid) VALUES (0, 'en', '');

INSERT INTO `users_field_data` (`uid`, `langcode`, `preferred_langcode`, `preferred_admin_langcode`, `name`, `pass`, `mail`, `timezone`, `status`, `created`, `changed`, `access`, `login`, `init`, `default_langcode`) VALUES (0, 'en', 'en', NULL, '', NULL, NULL, '', 0, 0, 0, 0, 0, NULL, 1);

e.g. via Drush:

drush -u 1 sql-query "INSERT INTO users (uid,  langcode, uuid) VALUES (0, 'en', '');"

drush -u 1 sql-query "INSERT INTO `users_field_data` (`uid`, `langcode`, `preferred_langcode`, `preferred_admin_langcode`, `name`, `pass`, `mail`, `timezone`, `status`, `created`, `changed`, `access`, `login`, `init`, `default_langcode`) VALUES (0, 'en', 'en', NULL, '', NULL, NULL, '', 0, 0, 0, 0, 0, NULL, 1);"

You can also do it in an update hook like:

function hook_update_n(&$sandbox) {
  $storage = \Drupal::entityTypeManager()->getStorage('user');
  // Insert a row for the anonymous user.
  $storage->create([
    'uid' => 0,
    'status' => 0,
    'name' => '',
  ])->save();
}

This will insert a new user with uid 0.

Check again:
select name, uid from users_field_data where name = '';
Output should be like this:

-+------+-----+
-| name | uid |
-+------+-----+
-|      |   0 |
-+------+-----+

Drupal 7

For Drupal 7, you can run two queries:

INSERT INTO users (name, pass, mail, theme, signature, language, init, timezone) VALUES ('', '', '', '', '', '', '', '');

UPDATE users SET uid = 0 WHERE name = '';

e.g. via Drush:

drush -u 1 sql-query "INSERT INTO users (name, pass, mail, theme, signature, language, init, timezone) VALUES ('', '', '', '', '', '', '', '')"

drush -u 1 sql-query "UPDATE users SET uid = 0 WHERE name = ''"

This will insert a new row getting a random UID and then the update query fixes to get the right uid for the user.

Check again:
select name, uid from users where name = '';
Output should be like this:

-+------+-----+
-| name | uid |
-+------+-----+
-|      |   0 |
-+------+-----+

Help improve this page

Page status: No known problems

You can: