I want to delete all content of custom entity type except some specific items, I found drush entity:delete CUSTOM_TYPE command to delete all contents but couldn't exclude some items from deletion in this command, does it have any option like --exclude ID1, ID2, ID3, ...?

Or is there any other way to do this?

  • 1
    It shouldn't be to difficult to add this option to the entity:delete command. You should open an issue as feature request on GitHub. – leymannx Sep 5 '19 at 5:32
  • 2
    I'm voting to close this question as off-topic because it's a feature request for Drush that should be handled in Drush's issue queue on GitHub. – leymannx Sep 5 '19 at 5:33
  • 1
    @leymannx I'm not just focusing on using entity:delete command, I asked for other solutions as well, the entity:delete command that I mention here is just the result of my researches before asking the question, so it doesn't seems to be an off-topic. – Mohandes Sep 5 '19 at 6:02
$ drush entity:delete node --bundle=article --exclude=9,14,81


$ drush entity:delete CUSTOM_TYPE --exclude=1,2,3

I added the --exclude option as a pull request #4175 to Drush. It also works in Drush 9, so you could use this pull request to patch your local Drush like that:

"patches": {
    "drush/drush": {
        "Add option to exclude entities by ID to entity:delete command.": "https://github.com/drush-ops/drush/pull/4175.patch"
| improve this answer | |

ok mate you want other options and here is the answer:

if you look at


you can see the what the function does....

now you need to learn how to make a custom drush commands i assume you are using drush 9 so you can just run:

"drush generate drush-command-file"

or read: https://www.axelerant.com/resources/team-blog/how-to-write-custom-drush-9-commands-for-drupal-8

this will create: modules/custom/YOURMODULE/drush.services.yml and modules/custom/YOURMODULE/src/Commands/YOURMODULECommands.php

now have a look at the following working example ...

 * Command delete some stuff
 * @param $entity_type
 *   Argument for entity type.
 * @param $exclude
 *   Argument for exclude delete of ids.
 * @param array $options
 *   An associative array of options whose values come from ...
 * @option option-name
 *   Description
 * @usage deletethings-commandName foo
 *   Usage description
 * @command deletethings:commandName
 * @aliases delete-my-stuff
 public function commandName($entity_type, $exclude,  $options = ['option-name' => 'default']) {
$exclude_ids= [];
if ($exclude !== 'none') {
  $exclude_ids = explode(',', $exclude);

// now do the delete stuff
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
if (is_object($storage)) {
  $all = $storage->loadMultiple();
  if (count($all) !== 0) {
    foreach ($all as $entity) {
      $id = $entity->id();
      if (!in_array($id, $exclude_ids)) {
        // kill with fire.

With that you can run:

drush delete-my-stuff ENTITY_TYPE 5,6,10

example: drush delete-my-stuff node 5,6,10

this would then allow you to delete all items of your entity except for "5,6,10" - which are ids you want to keep

or drush delete-my-stuff ENTITY_TYPE none (to delete all)

remember to run drush cr and drush cc (1) so drush will recognise the new command

| improve this answer | |

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.