Skip to main content

Bulk deleting Drupal nodes of a particular content type

An article from ComputerMinds - Building with Drupal in the UK since 2005
12th Apr 2008

Mike Dixon

Senior Mind
Mike Dixon
Hey, you seem to look at this article a lot! Why not Bookmark this article so you can find it easily in the future?

The Drupal admin interface allows you to delete up to 50 nodes at one time, which is great - but there are times when you it's just not enough and you need to bulk delete many thousands of nodes.

In this example we will delete all nodes of a particular type (page), a quick way to execute the code is to create a new node, set the input format to PHP, paste the code into the node body - add a title and click submit - don't forget to delete the node when your done!


  $node_type = 'page';
  
  //fetch the nodes we want to delete
  $result = db_query("SELECT nid FROM {node} WHERE type='%s'",$node_type);
  while ($row = db_fetch_object($result)){
    node_delete($row->nid);
    $deleted_count+=1;
  }
  //simple debug message so we can see what had been deleted.
  drupal_set_message("$deleted_count nodes have been deleted");

[EDIT] - Code changed to correct issues raised in comment #3

Hi, thanks for reading

ComputerMinds are the UK’s Drupal specialists with offices in Bristol and Coventry. We offer a range of Drupal services including Consultancy, Development, Training and Support. Whatever your Drupal problem, we can help.