Drupal – Delete all content nodes of a given type

By David Pratt / Tags: / 6 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

6 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.

theAgonizer February 10th, 2012 at 3:55 am

Where do I put the above devel code?
Love the imacro extention but if you are doing it on a remote site it can be very time intensive. It took 1 second per entry which ended up being 4 hours.
You can also use http://drupal.org/project/delete_all but after I installed it in drupal 6 I couldn’t find the administration panel.

you could also use drush and a command like this:

drush delete-all article

I don’t have access to the server so I don’t know what to do. Every solution I have tried is slow or does not work.

theAgonizer February 10th, 2012 at 4:06 am

I figured out how to use delete_all you have to go to admin/content/delete_content which did not appear in my admin menu.

Jiby John August 9th, 2012 at 8:17 am

There also an interesting alternative to do this,if you had enabled the Devel generate module.Go to devel generate content and select the desired content type and check the ‘Delete all content’. Don’t forgot to give the number of node to generate as 0. This will delete all nodes of that content type.

David Wilford October 9th, 2014 at 3:00 pm

Better to use a VBO called with Drush.
e.g.

drush vbo-execute si_vbo action::views_bulk_operations_delete_item

You can run this is linux ‘screen’ to avoid interruption. I’ve been using to remove over 500,000 complex entities.

Leave a reply