Last updated January 5, 2009. Created by robertgarrigos on November 1, 2008.
Edited by Jody Lynn. Log in to edit this page.
When developing on a new Drupal site you might need to create lots of test nodes. The developer module is of a great help for this, as well as deleting previous content. However, it deletes previous content only when creating new test content and it doesn't update sequences in any case, so you might end up having new nodes created with a very high nid, even if you only have few nodes.
To solve this I use this snippet, borrowed from the developer module, plus some new queries to update the sequences for nodes. You just need to replace the xxxxxx strings with the node types you want to delete.
<?php
$node_types = array(
'xxxxxx', //update this string with the type of node you want to delete
'xxxxxx', //update this string with the type of node you want to delete and add any more lines you might need
);
$sql = 'SELECT nid FROM {node} WHERE type IN ('. implode(', ', array_fill(0, count($node_types), "'%s'")). ')';
$result = db_query($sql, $node_types);
while ($row = db_fetch_object($result)) {
node_delete($row->nid);
}
db_query("UPDATE sequences SET id = (SELECT MAX(nid) FROM {node}) WHERE name = '{node}_nid'");
db_query("UPDATE sequences SET id = (SELECT MAX(vid) FROM {node_revisions}) WHERE name = '{node}_revisions_vid'");
?>
Comments
Update sequences
Hi,
thank's for the snippet.
Delete of the nodes worked fine for me,
but I got a problem with update sequences.
db_query("UPDATE sequences SET id = (SELECT MAX(nid) FROM {node}) WHERE name = '{node}_nid'");
produced this warning mesage in my MySQL database
#1146 - Table 'mydatabase.sequences' doesn't exist
And the value for autoincrement wasn't reset.
There is no table sequences in my database.
What I've found out is following statement:
"ALTER TABLE tbl_name AUTO_INCREMENT = 1"
I replaced the SQL in the db_query lines and it worked fine.
Just for somebody who maybe gets the same problem
Regards,
Hi, Could I with a sort of
Hi,
Could I with a sort of same query delete nodes which are not connected to terms from a certain vocabulary also?
Thanks a lot in advance for your reply!
greetings, Martijn