3 namespace Drush\Drupal\Commands\core;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drush\Commands\DrushCommands;
7 use Drush\Utils\StringUtils;
9 class EntityCommands extends DrushCommands
12 protected $entityTypeManager;
15 * EntityCommands constructor.
17 public function __construct(EntityTypeManagerInterface $entityTypeManager)
19 $this->entityTypeManager = $entityTypeManager;
23 * Delete content entities.
25 * To delete configuration entities, see config:delete command.
27 * @param string $entity_type An entity machine name.
28 * @param string $ids A comma delimited list of Ids.
29 * @param array $options
31 * @option bundle Restrict deletion to the specified bundle. Ignored when ids is specified.
32 * @usage drush entity:delete node --bundle=article
33 * Delete all article entities.
34 * @usage drush entity:delete shortcut
35 * Delete all shortcut entities.
36 * @usage drush entity:delete node 22,24
37 * Delete nodes 22 and 24.
39 * @command entity:delete
40 * @aliases edel,entity-delete
43 public function delete($entity_type, $ids = null, $options = ['bundle' => self::REQ])
45 $storage = $this->entityTypeManager->getStorage($entity_type);
46 if ($ids = StringUtils::csvToArray($ids)) {
47 $entities = $storage->loadMultiple($ids);
48 } elseif ($bundle = $options['bundle']) {
49 $bundleKey = $this->entityTypeManager->getDefinition($entity_type)->getKey('bundle');
50 $entities = $storage->loadByProperties([$bundleKey => $bundle]);
52 $entities = $storage->loadMultiple();
54 if (empty($entities)) {
55 throw new \Exception(dt('No matching entities found.'));
57 $storage->delete($entities);
58 $this->logger()->success(dt('Deleted !type entity Ids: !ids', ['!type' => $entity_type, '!ids' => implode(', ', array_keys($entities))]));