I have several thousand nodes of a particular content type. Using the web interface (example.com/admin/content), I can only delete about 50 at a time. How can I quickly delete them?

link|improve this question

feedback

7 Answers

up vote 18 down vote accepted

There is a module for that (TM).

See Bulk Delete.

That will use the Batch API to delete the nodes to avoid timeout or memory issues when deleting thousands of nodes with a single call to node_delete_multiple().

Bulk Delete is an abandoned module. See for alternatives:

link|improve this answer
Wow, I completely missed this one. Good find. – Greg Mar 9 '11 at 16:38
4  
One other method, that could be useful if you're doing more than a simple content type filter, is to use Views Bulk Operations: drupal.org/project/views_bulk_operations – geerlingguy Mar 9 '11 at 18:11
Bulk Delete module is abandoned now. It is suggested there to use Views Bulk Operations. – refineo Dec 13 '11 at 14:48
feedback

Looking at the Devel Generate module for inspiration, here is it's "content kill" function:

function devel_generate_content_kill($values) {
  $results = db_select('node', 'n')
              ->fields('n', array('nid'))
              ->condition('type', $values['node_types'], 'IN')
              ->execute();
  foreach ($results as $result) {
    $nids[] = $result->nid;
  }

  if (!empty($nids)) {
    node_delete_multiple($nids);
    drupal_set_message(t('Deleted %count nodes.', array('%count' => count($nids))));
  }
}

So I would try either using Devel Generate to delete all nodes but create no new ones, or use example.com/devel/php to call devel_generate_content_kill(array('node_types' => array('my_node_type'))); directly.

link|improve this answer
feedback

Create a file with below code in root of drupal installation and execute the file.

<?php
  require_once './includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

  $aquery= db_query("SELECT nid FROM {node} AS n WHERE n.type = 'company'");
  while ($row = db_fetch_object($aquery)) {
    node_delete($row->nid);
  }
?>
link|improve this answer
feedback

Views Bulk operations provides a BatchAPI enabled, configurable node admin screen that allows filtering by type, selection of all nodes matching your search criteria, etc.

That's my hands-down solution in Drupal 6 - besides batch delete, you can bulk-edit nodes and do a bunch of other stuff.

It looks like the Drupal 7 version isn't ready yet - but I'd be watching that module for a D7 release.

link|improve this answer
feedback

If you want to do it purely through the UI, you can use the devel_generate module.

  1. Navigate to the "Generate Content" menu in "Configuration" (admin/config/development/generate/content).
  2. Select the content types you want to delete.
  3. Make sure the checkbox next to "Delete all content in these content types before generating new content" is checked.
  4. Set the number of nodes you'd like to generate to "0".

This way, no nodes will be generated and all nodes of the selected types will be deleted.

link|improve this answer
feedback

I ended up using db_delete, no modules required:

<?php
  db_delete('node')
    ->condition('type', 'MY_CONTENT_TYPE')
    ->execute();
?>

Edit/Warning: See Berdir's comment below. This method does not clean up all data related to the nodes.

link|improve this answer
3  
The problem with this approach is that other modules are not able to react and delete their information too. Depending on the content type, there is node specific information in the content_type table of that node, fields, taxonomies (a field too, actually), comments and so on. So you possibly end up with a lot of stale information in your database. – Berdir Mar 9 '11 at 17:47
@Berdir - Interesting. Is there a better built-in way to delete nodes? – Greg Mar 9 '11 at 17:56
3  
There is node_delete() and node_delete_multiple(), see tim.plunket's answer, but that isn't designed to handle thousands of nodes, see api.drupal.org/api/drupal/modules--node--node.module/function/…. It loads all nodes and then iterates over them in a single request. So pretty much have to use a contrib module like bulkdelete if you want to delete hundreds or thousands of nodes. – Berdir Mar 9 '11 at 18:20
feedback

Another snippet is:

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

where TO_BE_DELETED is the content type to be deleted.

link|improve this answer
1  
It should be noted that works for Drupal 4.6, 5, and 6 only. The Drupal 7 version is db_delete('node') – Greg Mar 10 '11 at 4:44
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.