[Drupal] [SOLVED] How to restore Anonymous user record in Drupal?

| | 1 min read

There are many ways to get delete the Anonymous user account from the Users table. A wrong query like 'uid != 0' or Views bulk operations may cause deleting the row.

While working in a hacked site, we got an error in drush like

"Could not login with user ID #0."

We can fix this by doing 2 quick steps. Run the following queries in your Database.

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

This will create a new row with uid as auto increment and all other fields as NULL.

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

For Drupal 6, we can take a little bit different approach.

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

Specify the uid as 0 in the INSERT query.

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);

Hope this helps.