Last updated July 5, 2015. Created on January 17, 2011.
Edited by azovsky, kostajh, greggles, LeeHunter. Log in to edit this page.

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/ on line 1181." and various other strange errors from Drupal core

Fixing cases where you accidentally deleted the user

There are many ways to accidentally delete the uid 0 (Anonymous) user: a mistaken query that forgot to use "and uid != 0" or a Views Bulk Operations view that was a little too aggressive.

The technique to fix it depends on a few factors. Below are different techniques that work on Drupal 7 or Drupal 6.

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.

For Drupal 6 using a slightly different technique

You can also insert directly with a specific value for the auto-incrementing UID if you tell your database to use a different SQL_MODE.

This pair of statements will bring the user back on Drupal 6:

INSERT INTO `users` (`uid`, `name`, `pass`, `mail`, `mode`, `sort`, `threshold`, `theme`, `signature`, `created`, `access`, `login`, `status`, `timezone`, `language`, `picture`, `init`, `data`)
VALUES ('0', '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, NULL, '', '', '', NULL);

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

-| name | uid |
-|      |   0 |
-1 row in set (0.00 sec)

Fixing cases where the 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:

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 |
-1 row in set (0.00 sec)

Looking for support? Visit the forums, or join #drupal-support in IRC.


majusz’s picture

Thanks for this comment!
It just solved a problem I was trying to fix for the last two days.

I had this issue with a Drupal 6 installation that the search index wasn't properly rebuilt by the usual cron job. After researching a lot and not finding anything helpful, I installed Drush, and Drush told me that it couldn't login as user #0. So I followed the directions above, so that I could use Drush with user #0. After I did that, Drush AND the search indexing worked perfectly fine.

So for everyone having similar problems: the Drupal search index rebuilding engine seems to rely on the Anonymous user as well.

danieltome’s picture

If you came to this issue because Drush sent you here:

Drush spits out:

Could not login with user ID #0. This is typically caused by importing a MySQL database dump [error]
from a faulty tool which re-numbered the anonymous user ID in the users table. See for help recovering from this situation.

This was related to a site we built where we do not allow anonymous access. So the homepage displays access denied and the login screen.

Simple solution is to run drush as admin:

Eg clear cache:
drush -u 1 cc all

kraigory’s picture

Running drush as user 1 worked wonderfully for me.

drush -u 1 cc all

Does anyone know if there are any downsides/dangers to running drush as admin user? If not, is there a way to tell drush to ALWAYS run as admin?

emcniece’s picture

Man, I wish there was an upvote system. Nice tip!

dags’s picture

This error suddenly started appearing while I was trying to debug migrations from the command line using XDebug + PHPStorm. The problem was that I had set a bad conditional breakpoint on a line in /includes/ -- I had used the assignment operator where I should have used the equality operator (ie. $ids=NULL instead of $ids==NULL). Big facepalm. Be careful when using debuggers!

m1r1k’s picture

Well, as now Drupal 7 has INNER JOIN to user_revision table, we have to keep connection between users and user_revision tables on vid, otherwise user_load won't return any user.

rooby’s picture

Only if you are using the user revision module.

highermath’s picture

This was exactly my issue. The VID for user 0 did not mach the one in the user table.

This might reflect a bug in the user revision module.

Thanks for this.

mohangupta’s picture

What is the remedy for Drupal 8?

wizbard’s picture

I know very little PHP - just enough to be dangerous. I'm specifically trying to get Ubercart working, but after each completed transaction (membership registration), User 0 is re-numbered. Yes, I can go run the query in PHPMyAdmin manually, but in what file should I insert the snippet listed above:

insert into users (name, pass, mail, theme, signature, language, init, timezone) values ('', '', '', '', '', '', '', '');
update users set uid = 0 where name = '';

to restore User 0 after each new transaction? I'm using Drupal 7.

Many thanks for all the help I've received from these forums!


goose2000’s picture

I just updated to Drupal 6.32 and this seems to have broke Drush now. I'm getting this error now.

svetlio’s picture

You have uid0 and have this err?
(MYSQL related)

1. export your db_name.
2. drop your db_name.
3. create db_name with utf8 charset.
CREATE DATABASE `mydb` CHARACTER SET utf8 COLLATE utf8_general_ci;
4. import your db.

wizbard’s picture

Thank you, svetlio. I just saw this response from you and will try it. But before I do (since I just crashed another site of mine because of a different problem relating to dropping the database) let me just confirm that you are basically just telling me to delete and then recreate the database (after backing it up) using the utf8 charset. Correct? Sorry, I just want to do it right the first time.



svetlio’s picture

That's right

One note from my experience:
One of my installation this was good effect, for another not. So this is not the 100% for all situations solution. But sometime work ;)

JurgenR’s picture

Full time Drupal 7 backend developer since 2011.
Open Source Webdevelopment Coding Blog

sujann143’s picture

I didnt realize about it but while i was using Quiz module, i was not able to proceed to next question after i click next. I did mysql import on my local and while i went to do drush cc, it gave me message of missing uid 0 and that's how i realised the issue with it.

mariocantor’s picture

In my Drupal 7 the SQL command did not worked, so I removed fields (mode`, `sort`, `threshold`) and their values. and the query worked perfectly.

divined’s picture

Most value question: Why its happens?

Why UID 0 is removed from the database once a month?