Drupal - How to delete a node or a list of nodes with Drush?

Finally I create my own module named drush_delete

Inside the drush_delete.drush.inc file put this code:

<?php
/**
 * @file
 * The Drush Delete drush commands.
 */

/**
* Implements hook_drush_command().
*/
function drush_delete_drush_command() {
  $items['node-delete'] = array(
    'description' => dt("Delete nodes."),
    'aliases' => array('nd'),
    'arguments' => array(
      'nids' => dt('The nids of the nodes to delete'),
    ),
    'examples' => array(
      'drush node-delete 1' => dt('Delete the node with nid = 1.'),
      'drush node-delete 1 2 3' => dt('Delete the nodes with nid = 1, 2 and 3.'),

    ),
  );
  return $items;
}

/**
 * Callback for the node-delete command
 */
function drush_drush_delete_node_delete() {
  $nids = func_get_args();
  $nids = array_filter($nids, 'is_numeric');
  $nids = array_map('intval', $nids);
  $nids = array_unique($nids);
  $nids = array_values($nids);
  $cant = count($nids);

  if ($cant > 0) {
    node_delete_multiple($nids);

    drush_print(dt("Deleted nodes:"));
    drush_print(implode(' ', $nids));
  }
  else {
    drush_set_error('DRUSH_ERROR_CODE', dt("You must enter at least one nid"));
  }
}

Install the module, run drush cc drush to clear the drush cache and use the command like this:

To delete a node use:

drush node-delete 1
drush nd 1

To delete multiple nodes use:

drush node-delete 1 2 3
drush nd 1 2 3

You can found the command in this module:

https://github.com/adrian-cid/drush_commands


If you use the Drush Entity module, then you may run drush entity-delete node 123 to delete nid 123 from your site.

EDIT: If somebody need to use the drush entity-delete command, should use the dev version of the module: https://www.drupal.org/project/drush_entity/releases/7.x-5.x-dev


IMHO the easiest way is with php-eval:

drush php-eval "node_delete_multiple(array(NODE_ID));"

...

drush php-eval "node_delete_multiple(array(34));"     // for node/34

drush php-eval "node_delete_multiple(array(34, 35));" // for node ids 34 and 35

Tags:

Nodes

Drush

7