8

I discovered the Delete all module which gives you the ability to delete all the nodes of a given content type. However, with this module you can't delete a single node or a list of nodes.

In the question "Can I delete nodes of a given content type with Drush?" I found 2 answers talking about the command drush node_delete <nid> but I can't find this command.

I found in a discussion about node_delete() some code using eval to accomplish this task, but it is using eval:

drush php-eval "global \$user; \$user = user_load(1); node_delete(nid);"

In case that there doesn't exist a functionality implemented to accomplish this, what is the better way to implement this functionality: in the Drush module or create a new module?

0

4 Answers 4

6

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

4
7

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

7

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
2
  • Thanks, but I think that if you have a drush command you can add arguments and option that maybe are needed. And you can add validations in a easy way. Oct 17, 2017 at 12:01
  • I love this answer. No custom drush command needed. Sep 5, 2019 at 17:55
1

If you want to delete all nodes use the range() function as

drush php-eval "node_delete_multiple(range(1,3000));"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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