Author image
Senior Mind

Bulk deleting Drupal nodes of a particular content type

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

Comments

Thanks for the article, very handy =)

If I may make a suggestion, rather than doing this in a node, which is always risky using php, and since this is a one time thing anyway, a better place to do this would be in the php block of devel, or in /devel/php, asuming you have the devel module installed.

Yup, absolutely the devel module's PHP execute block is the place to do this sort of thing ... we tend not to have devel on production sites though and sometimes a bit of sneaky PHP in a node can work wonders :)

1) Scripts like this are likely to timeout, so you may want to add a set_time_limit() to this.
2) There are single quotes in the last line of the script which should be double quotes, or else $deleted_count is not rendered.

My version:

<?php
$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)){
    set_time_limit(20);
    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");
?>

Guys I've tried your code but I am getting some parsing error.

I'm using Drupal 6.

<b>Parse error</b>:

parse error in <b>C:\wamp\www\anileverywun\sites\all\modules\contrib\devel\devel.module(1058) : eval()'d code</b> on line <b>1</b><br /

I'm guessing this is simply a problem due to some Devel problems - your help would be appreciated.

Thanks,
-Anil

If your using the devel PHP block thing, then you need to make sure you do not include the opening and closing PHP tags, i.e. remove <?php and ?> from the above.

Cheers,

Mike

thank you for the simple, clean code and clear instructions!.. very helpful for new drupal-devs/admins like me :)

btw there is also some nice code for doing this at http://drupal.org/node/92861

if you scroll down to near the end, there is the drupal 6 version.

Comments on this article are now closed, if you want to give us feeback you can use our contact form instead.