14

Just wondering if Drush is capable of deleting nodes of a given content type.

Something like: $ drush delete-node --type=MyContentType

If not possible, can I create a method like that?

14 Answers 14

5

You could create a function like follows:

function MYMODULE_delete_all_the_things() {
  $query = new EntityFieldQuery;

  $result = $query->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'YOUR-CONTENT-TYPE')
    ->execute();

  if (isset($result['node']) && count($result['node'])) {

    $node_ids = array_keys($result['node']);

    node_delete_multiple($node_ids);
  }
}

Note - You could also use a simple SELECT query, but seeing as you're working with entities it seems more sensible/common-practice to use EntityFieldQuery.

This should also be quite easy to change or add arguments to. You can also put it in a Drush command quite easily - there's an example for how to do this in the Drush repository (check the Commands section near the bottom).

  • I see, it sound like the repository link shows how to properly add a drush method. – chefnelone Jun 14 '12 at 11:57
  • Yup! The readme has some different ways you can implement them as well! – Chapabu Jun 14 '12 at 12:18
21

Install the devel module, and use drush to delete all the nodes,

$  drush genc --kill 0 0 

You can also give a type option,

$  drush genc --kill --types=article 0 0
  • Clever, I like it. – m4olivei Dec 29 '14 at 19:19
  • 3
    This works with D8. The devel_generate module is included with devel, but needs to be enabled with drush en devel_generate genc is normally used to create nodes, so the 0 0 at the end is telling it not to create anything new, and the --kill to remove what's already there. – William Turrell Dec 14 '16 at 11:24
16

I guess you can by using the below command

drush node_delete <nid>

EDIT: Found a module that does something/related to the question

http://drupal.org/project/delete_all

Usage

Drush

drush delete-all

Example: drush delete-all article

6

Something like this would work (untested):

$query = new EntityFieldQuery();

$entities = $query->entityCondition('entity_type', 'node', '=')
  ->entityCondition('bundle', 'Announcements')
  ->execute();

$nids = array_keys($entities['node']);

node_delete_multiple($nids);

This will find all of the nodes with the content type Annoucements using EntityFieldQuery(). It then gets all the $nids from the result, and deleted them using node_delete_multiple().

You can take this code, put it in a separate PHP file, and then execute it with drush scr.

Using the API will ensure that all of the proper hooks fire. Among other things, they will also delete the node revisions and field data (and their revisions) so you don't have orphaned data in the database.

6

There are some good ideas on this thread. If you dont want to do any programming really and want to use Drush you can look at the Delete All contributed module:

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
5

VBO has Drush integration. Create a VBO view of nodes, execute it via Drush (using drush vbo-execute), pass the node type as an argument.

  • I think that this is what I need.Just 2 questions: Which is the drush method to execute a view? and Will this method be faster than executing the view in the browser? – chefnelone Jun 14 '12 at 11:47
  • Edited the answer to specify the exact drush command. And yes, it will be faster because this way the view isn't rendered. – Bojan Zivanovic Jun 15 '12 at 12:10
4

Drush 9

drush entity:delete node --bundle=my_content_type

  • 2
    Best answer nowadays! – Jonathan Apr 18 at 12:03
2

You can also create a drush script (let's say it's called "bulk_delete.php" and is placed under the Drupal root folder):

#!/usr/bin/env drush

$res = db_delete('node')
  ->condition('type', 'mycontenttype', '=')
  ->execute();

echo "deleted:" . $res;

This is absolutely the fastest way to do it: a direct query to the db, using the Drupal's function db_delete

Usage:

:~# cd /var/www/www.mysite.com
:~# drush --uri=www.mysite.com scr bulk_delete.php 

Doc: https://api.drupal.org/api/drupal/includes!database!database.inc/function/db_delete/7

Warning: this process does not delete the data of their fields. Anyway the same process can be applied to these fields (looking at the "bundle" column of each field). For example:

#!/usr/bin/env drush

$field_tables = array(
  'field_data_field_body',
  'field_data_field_mycoolfield',
  'field_revision_body',
  'field_revision_field_mycoolfield'
);

foreach ($field_tables as $field_table) {
  $res = db_delete($field_table)
    ->condition('bundle', 'mycontenttype', '=')
    ->execute();

  echo "deleted:" . $res . "\n\n";
}

Where "mycontenttype" is the one used in the first query.

  • Why the condition 'nid' > 5000? – Елин Й. Oct 8 '13 at 17:12
  • it's an example, like the type. – Mauro Mascia Oct 8 '13 at 17:23
  • But the original poster doesn't mention about such a condition, that's why I think the example is irrelevant, thus misleading. – Елин Й. Oct 8 '13 at 17:31
2

Drupal 7

Try the following one-liner, it removes all MyContentType nodes from Drupal:

drush eval '$res = (new EntityFieldQuery)->entityCondition("entity_type", "node")->entityCondition('bundle', 'MyContentType')->execute(); entity_delete_multiple("node", array_keys(reset($res)));'

Or you can remove all the entities based on the entity type name (e.g. node):

drush eval '$res = (new EntityFieldQuery)->entityCondition("entity_type", "node")->execute(); entity_delete_multiple("node", array_keys(reset($res)));'

Note: node is your entity type name, you can change it if required.


If you'll get memory or time out errors, you may add the following sets just before the $res:

ini_set('memory_limit', -1); ini_set('max_execution_time', 0);
2

To delete all nodes of a particular content type, you may try to run the following command via drush:

drush eval '$res = (new EntityFieldQuery)->entityCondition("entity_type", "node")->entityCondition('bundle', 'MyContentType')->execute(); entity_delete_multiple("node", array_keys(reset($res)));'

Where MyContentType is your machine Content Type name (e.g. Page).

2

For individual nodes:

$ drush php-eval 'node_delete($node->nid);'

drush php-eval
Evaluate arbitrary php code after bootstrapping Drupal

  • Please add some more details to explain your code, without that this may be considered as a low quality answer (and hence it might get deleted via moderation). – Pierre.Vriens Nov 25 '16 at 8:32
1

You can use

drush node_delete NID to delete specific node, but if you want to delete all nodes by content type I guess you can create a drush plugin using batch api.

  • I can't found the drush node_delete NID command. – Adrian Cid Almaguer Jun 22 '16 at 14:44
1

You can also do this with the devel module as answered at Can I delete nodes of a given content type with Drush?

Install the devel module, and use drush to delete all the nodes,

$ drush genc --kill 0 0

You can also give a type option,

$ drush genc --kill --types=article 0 0

1

Updating the answer provided by @kenorb.

In Drupal 8

drush eval '$nids = \Drupal::entityQuery('node')->execute(); $storage = Drupal::entityTypeManager()->getStorage('node'); $e = $storage->loadMultiple($nids); $storage->delete($e);'
  • drush ev '$type='\''node'\''; entity_delete_multiple($type, \Drupal::entityQuery($type)->execute());' – geek-merlin May 3 '18 at 13:04
  • To update an existing answer, please drop a comment to suggest an edit. – leymannx Oct 18 '18 at 8:09

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.