31

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?

17 Answers 17

32

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:

  • 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
26

Looking at the Devel Generate module for inspiration, here is its "content kill" function devel_generate_content_kill:

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.

18

In Drupal 8 one way is use the entityQuery() method with the EntityStorageInterface::delete() method:

$result = \Drupal::entityQuery("node")
    ->condition("type", "YOUR_CONTENT_TYPE_NAME")
    // If the update is being executed via drush entityQuery will only
    // return the content uid 0 have access to. To return all set
    // accessCheck to false since it defaults to TRUE. 
    ->accessCheck(FALSE)
    ->execute();

$storage_handler = \Drupal::entityTypeManager()->getStorage("node");
$entities = $storage_handler->loadMultiple($result);
$storage_handler->delete($entities);

If you need to apply other filters/conditions you can check the QueryInterface interface page

EDIT (Other way, thanks to @4k4):

$storage_handler = \Drupal::entityTypeManager()->getStorage("node");
$entities = $storage_handler->loadByProperties(["type" => "YOUR_CONTENT_TYPE_NAME"]);
$storage_handler->delete($entities);

If you want to test the code you can use:

drush php-eval '$storage_handler = \Drupal::entityTypeManager()->getStorage("node"); $entities = $storage_handler->loadByProperties(["type" => "article"]); $storage_handler->delete($entities);'

This will delete all your articles.

  • This is not correct, delete() expects loaded entities, not ID's, which is what entity query returns. Instead of a non-descriptive $result, use $ids or $nids and then $storage->delete($storage->load($ids)). – Berdir Dec 30 '16 at 22:22
  • @Berdir thanks, I will test the code again, maybe this problem was introduced in one edit – Adrian Cid Almaguer Dec 30 '16 at 22:23
  • @Berdir I update the answer and I test the code and it works, the problem was introduced in one edit. – Adrian Cid Almaguer Dec 30 '16 at 22:31
  • 2
    @AdrianCidAlmaguer, you could replace the query with $entities = $storage_handler->loadByProperties(['type' => 'YOUR_CONTENT_TYPE_NAME']); – 4k4 Dec 30 '16 at 22:59
  • 1
    Recently used this in an update hook, it's worth noting that if you use entityQuery you should set the accessCheck on the statement. Otherwise if you run it in drush, by default accessCheck is set to true and any nodes that uid 0 does not have access to will not be returned. – awm Aug 22 '18 at 16:07
13

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.

10

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);
  }
?>
  • 1
    db_fetch_object is not part of D7 database api. – ya.teck Dec 25 '13 at 8:45
10

You can do this is in Drupal 7 using the Execute PHP Code part of the Devel module by entering:

$result= db_query("SELECT nid FROM {node} AS n WHERE n.type = 'TYPE'");
foreach ($result as $record) {
  node_delete($record->nid);
}
  • 1
    This will execute very slowly, if you need to delete thousands of nodes it's better to use a method which uses the batch API such as VBO. – abielefeldt Mar 14 '17 at 15:47
6

Do this in terminal, if you use Drush and the delete all module:

 drush delete-all [content-type-machine-name]


Examples:
 drush delete-all article             Delect all article nodes.
 drush delete-all all                 Delete nodes of all types.
 drush delete-all --reset             Delete nodes of all types, and reset node, revision and comment counters.
 drush delete-all users               Delete users.

Options:
 --reset                              Reset counter for node, revision and comment tables.
 --roles                              pick roles
5

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.

4

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.

  • 3
    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
4

With Devel module, using drush:

drush genc 0 --types=article --kill

Or in UI as explained here: http://befused.com/drupal/delete-nodes-devel

  • It's the Devel sub-module devel_generate that needs to be enabled. And for multiple types it's drush genc 0 --kill --types="article, page" – leymannx Nov 21 '18 at 21:39
3

I use Delete all module, it works fine with D8 and provides very useful drush commands. For example, to delete all content of the article content type:

drush delete-all article  
  • Does this work with D8? I enabled the module but I get The drush command 'delete-all article' could not be found. I also cleared the drush cache – Yassin Tahtah Jan 11 '17 at 14:39
1

You can try Delete all module, navigate to 'admin/content/delete_content' and you will be presented with a form for deletion of content belonging to certain content types.

Regards

0

If you have Migrate module enabled you can use:

$ drush migrate-wipe <content-type>

See Typical migrate commands using Drush.

0

This module is used to delete all content and/or users from a site. This is mainly a developer tool, which can come in handy in several case

https://www.drupal.org/project/delete_all

as will as the Bulk Delete module will delete any nodes of a certain node-type using the batch api. It is advised to use the Views Batch Operations module (VBO) for a small number of nodes. But if you have to delete 10.000 nodes this module might be a better option.

https://www.drupal.org/project/bulkdelete

0

Delete all nodes of content type programmatically here is a helper function:


function _delete_all_nodes_of_type($type = '') {
  // Return all nids of nodes of type.
  $nids = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->condition('n.type', $type)
    ->execute()
    ->fetchCol(); // returns an indexed array
  if (!empty($nids)) {
    node_delete_multiple($nids);
    drupal_set_message(format_plural(count($nids), count($nids) . ' node Deleted.', count($nids) . ' nodes Deleted.'));
  }
}

-1

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.

  • 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
-1

If you dont want to cod,e you can try this module, https://drupal.org/project/total_control

Just go to Dashboard -> Content, select all content (you may filter by content type), then select "Delete item"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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