Drupal Bulk Delete Nodes and Reset NID Counter

Category: 

Here is how to bulk delete a big chunk of nodes for a particular content type in Drupal. This code snippet was taken from here and modified to set a limit so php won't run out of memory and then it tells you how many nodes are still left for that content type. This is easiest to run inside the Devel's module PHP block.

$node_type = 'dg_hole';
 
//fetch the nodes we want to delete
$result = db_query("SELECT nid FROM {node} WHERE type='%s' LIMIT 1000 ",$node_type);
while ($row = db_fetch_object($result)){
  node_delete($row->nid);
  $deleted_count+=1;
}
//simple debug message so we can see what had been deleted.
drupal_set_message("$deleted_count $node_type nodes have been deleted");
drupal_set_message(db_result(db_query(" SELECT COUNT(nid) FROM {node} WHERE type='%s' ",$node_type)) . " $node_type nodes left");

When you're done you may want to reset your nid counter to particular number, here is how you can do it: (this code snippet was taken from here)

ALTER TABLE  node AUTO_INCREMENT=1

And then if you want to, don't forget to update the node revisions vid counter to the appropriate place. Make sure you know what VID to set for the auto increment, VIDs will be different from NIDs if you have any node revisions turned on!

ALTER TABLE  node_revisions AUTO_INCREMENT=1

Comments

thanks!
I forgot the ALTER TABLE node_revisions AUTO_INCREMENT=1 :-)

Thanks for your post.

I leveraged delete_all module; however the ALTER TABLE command is key. Also I'd suggest the below link when resetting the User table as it covers the nuances with the sequence table.

https://www.drupal.org/node/288065

Add new comment