Error message

The spam filter installed on this site is currently unavailable. Per site policy, we are unable to accept new submissions until that problem is resolved. Please try resubmitting the form in a couple of minutes.

Drupal Bulk Delete Nodes and Reset NID Counter


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)){
//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)


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!



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.

Add new comment