Drupal – Delete all content nodes of a given type

By David Pratt / Tags: / 2 Comments / Published: 28-07-09

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);
}

Careful!

UPDATE:
There is a far better way of doing this: 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('event')));

Category: Tech

Tags:

Posted: on July 28th, 2009 at 12:47 pm.

Feeds: RSS 2.0

2 Responses to “Drupal – Delete all content nodes of a given type”

Nelson September 13th, 2010 at 10:07 pm

Another way to do the job (especially for someone who is not a fan of coding) is to use iMacro Firefox extension.

Record the deletion of one page and play it to delete the rest.

As you said, though, one should be very carefull on this.

woo October 28th, 2011 at 11:43 am

That’s an amazing little firefox extension Nelson, simple way to automate the process of deleting thousands of nodes through views bulk operations or through the normal content management feature.

Leave a reply