Drupal – Delete all content nodes of a given type

If you have thousands of nodes in your Drupal 6 CMS, there is no easy way from within the admin area to delete large numbers of nodes of a given type. If you rely on the GUI you will end up having to do dozens of “select all, delete” iterations to get through it all.

This sneaky bit of code will delete all records of a particular type (just replace the #### appropriately):

$query = db_query("SELECT n.nid FROM {node} n WHERE n.type = '####'");
while ($n = db_fetch_object($query)) {
    node_delete($n->nid);
}

The easiest way to run this code is to add it to the cron hook by declaring the hook_cron() function in any of your modules:

function your_module_cron() {
// add the code here
}

and then run cron manually from the admin interface or just enter the following URL:

http://www.yoursite.com/cron.php

There is a far better way of deleting all the nodes: if you activate the Devel module, and then use the following line of code:

//Delete all data of content type "event":
devel_generate_content_kill(array('node_types' => array('my_node_type')));
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s