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?
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).
drush entity:delete node --bundle=my_content_type
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 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. 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
delete_all isn't yet ported to Drupal 8, but the genc answer works. 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.
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
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.
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.
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); 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).
For individual nodes:
$ drush php-eval 'node_delete($node->nid);' drush php-eval
Evaluate arbitrary php code after bootstrapping Drupal
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);' To help those that find this thread - use the following command to delete all the nodes within a content type - drush uses bundles now instead of types:
drush genc --kill --bundles=about 0 0 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
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.