Drupal Bulk Deleting Content

When developing a Drupal website sometimes we are migrating content many times until the content is properly migrated into Drupal. Or, sometimes we create a bunch of dummy Lorem Ipsum filled content as example content that could be on the website. When doing these tasks it can be useful to know of a couple of means to delete bulk content.

When I say Delete, I mean purge forever from Drupal — there is no undoing this operation. By default Drupal supports Published/Unpublished content. There are other options available when looking to support Drafts in Drupal. Such as Workflow, Workbench, State Machine and Maestro.

3 Example Methods to Bulk Delete Content in Drupal

Default Drupal Content Administration Page

Out of the box site administrators can delete content by type at the Admin -> Content page. You may filter by type(s) and then Delete the content. But, by default this page limits the number of results to 50. If you want to tweak this value you would override a limit in modules/node/node.admin.inc module file to a greater number. For some, this solution may work fine and is of little hassle.

Default Drupal Admin Content Delete Page

Default Drupal Admin Content Delete Page

Another option for developers of a website is essentially what the devel_generate module does. Devel Generate will let you create dummy content — it may also Delete all content of certain type(s) if you request it to do so. Devel Generate is a part of the Devel contributed module.

Devel Generate Add/Delete Dummy Content Admin Page

Devel Generate Add/Delete Dummy Content Admin Page

Custom EntityFieldQuery and Drush

If we look at the source of the Devel module we could see that an EntityFieldQuery is used to delete all the related content. A simple Drush script with this simple query is all we need to perform mass deletions:

<?php

// A custom PHP script used by Drush place in
// for example /sites/all/modules/kill_content.php

function custom_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))));
  }
}

$values['node_types'] = array('article', 'page');
custom_content_kill($values);

Then we invoke this file via Drush:

$> drush php-script kill_content.php

Delete All Contributed Module

Lastly, if the above isn’t flexible enough for you and you want a module to install and simply say “Please delete everything of content_type X,Y,Z” then the Delete All module is for you. It uses Drush to support deleting of all content to avoid timeout of Apache. This module attempts to make full use of the Drupal API so all the proper hooks should fire as it typically uses node_delete. It also supports a slightly less way of deleting massive amounts of content quickly via SQL-joins. Since this method bypasses many of the Drupal hooks it’s not recommended; but for huge datasets it can be very helpful.

Usage

Drush
drush delete-all
Example: drush delete-all article

Drush on Drupal 7 version

Delete all nodes, nodes of a specific content type or users.

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

Aliases: da

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under Drupal, Drush | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

Leave a Reply

Your email address will not be published. Required fields are marked *


× 5 = 45

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>