17

I generated tons of dummy content for testing, and then deleted them all. As nid is auto-increment, newly created node will have a nid of last created nid +1.

How can I make nid start from 1 again?

  • 6
    The question is, why would want to do that. It doesn't really matter what nid is used, and there is no guarantee that it is always strictly sequential anyway. – Berdir Mar 19 '11 at 19:52
  • 3
    I agree with Berdir; no module should rely on the fact the first node has ID equal to 1. This is similarly to the user ID of the users: you can rely on the presence of user with ID equal to 0 and to 1, which are always created from Drupal during installation, but you should not rely on the presence of a user account with ID equal to 3. Other modules could delete the account of the anonymous user or the super-user, but that should be considered a bug of such modules. – kiamlaluno Mar 20 '11 at 13:19
13

Plase don't truncate the node table just like that, there are some other tables connected to the node table, like node_revisions, sequences, fields tables, taxonomy tables, and many more.

There is no secure way to do this, it really depends on your site, you'll have to truncate and adjust all tables that have an nid including the sequences table. Remember, it is highly probable that your site become unusable, so don't start without a backup first.

  • 1
    I would expect that other tables that contains references to nodes should be empty too, when all the nodes are deleted. Said that, no module should rely on the fact the first node has ID equal to 1. – kiamlaluno Mar 20 '11 at 13:10
16
ALTER TABLE `node` AUTO_INCREMENT = 1;

P.S - Others who have replied on this question: I just did a small check after deleting content and the associated content is indeed deleted, node_revisions and field_* tables are empty. node_delete says the same - http://api.drupal.org/api/drupal/modules--node--node.module/function/node_delete_multiple/7

Sometimes on migrations projects when you migrate from other platforms (ex. ASP + MSSQL), sometimes it becomes unavoidable to reset the nid due to lot of factors. I am sure it could be avoided but time/code tradeoff is always there.

  • I've been going through a massive migration project that's been taking months. The site I've been using for development and testing has had some new nodes added here and there, which hoses some of the more recent content. I'm thinking you'd need to roll back all nodes and comments, and set the AUTO_INCREMENT = 1 for both. I'm wondering if anyone that has done this for migrations has run into any other issues, especially for Drupal 7? – Webdrips Feb 18 '17 at 8:23
2

SQL Query: ALTER TABLE 'node' AUTO_INCREMENT=1;

Make sure you have deleted all nodes before doing this.

2

You can use the module Delete all with the following drush commands:

Drupal 7

Delete nodes of all types, and reset node, revision and comment counters.

drush delete-all --reset

Drupal 8

see here

Deletes all content of all types.

drush delete-all-delete-content

And then:

ALTER TABLE `node` AUTO_INCREMENT=1;
ALTER TABLE `node_revision` AUTO_INCREMENT=1;
ALTER TABLE `node_field_data` AUTO_INCREMENT=1;
ALTER TABLE `node_field_revision` AUTO_INCREMENT=1;
0

you can truncate node table but, pay attention you must truncate relational field table, if you just want to start increment id from 1 you can use of

ALTER TABLE  `node` AUTO_INCREMENT=1
  • Truncating node table will ignore all hooks that should fire on node delete, so it is not a wise idea. For example, it will leave data in revisions table, and Drupal will crash when it will hit the already used nid trying to create revision. – Mołot Oct 1 '14 at 8:47
  • @Mołot if you truncate node and revision node table you don't have any problem. I test it and don't have problem – shahab Oct 1 '14 at 13:22
  • The fact that in your specific setup this method succeeded does not prove it will always work. – Mołot Oct 1 '14 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.