Deleting nodes of a given content type with Drush

To delete nodes of a given content type with Drush, you can use the following commands depending on your Drupal and Drush version. For Drush 9 and Drupal 8/9:

drush entity:delete node --bundle=my_content_type

Replace "my_content_type" with the machine name of your content type. For Drush 8 and Drupal 7, you can use a custom script:

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

Replace "YOUR_CONTENT_TYPE" with the machine name of your content type. This function can be added to a custom module and called using a custom Drush command. Alternatively, you can try the following command for Drupal 7:


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

Replace "MyContentType" with the machine name of your content type. For more details on how to create a custom Drush command, refer to the Drush repository and the Commands section near the bottom.

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!