Skip to content

Instantly share code, notes, and snippets.

@steinmb
Last active May 16, 2020 10:10
Show Gist options
  • Select an option

  • Save steinmb/3afe0138dbb4537e7618324c2ee2b730 to your computer and use it in GitHub Desktop.

Select an option

Save steinmb/3afe0138dbb4537e7618324c2ee2b730 to your computer and use it in GitHub Desktop.

Revisions

  1. steinmb revised this gist May 16, 2020. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions CleanUp.php
    Original file line number Diff line number Diff line change
    @@ -35,18 +35,24 @@ public function removeEntities()
    print 'Deleted ' . count($entities) . ' of type ' . $this->entityType . PHP_EOL;
    }

    public function summarise() {
    }

    final class Utils
    {
    private function __construct() {}

    static function summarise()
    {
    foreach (\Drupal::entityTypeManager()->getDefinitions() as $entityType) {
    $entityTypeToClean = new self($entityType->id());
    $entityTypeToClean = new CleanUp($entityType->id());
    print count($entityTypeToClean->findEntities()) . "\t" . $entityType->id() . PHP_EOL;
    }
    }

    }

    // Example use.
    $entityType = 'node';
    Utils::summarise();
    $entityType = 'file';
    $entityTypeToClean = new CleanUp($entityType);
    $entityTypeToClean->summarise();
    //print count($entityTypeToClean->findEntities()) . "\t" . $entityType . PHP_EOL;
    //Warning! $entityTypeToClean->removeEntities();
    print 'Found ' . count($entityTypeToClean->findEntities()) . ' ' . $entityType . '(s)' . PHP_EOL;
    //Danger zone! $entityTypeToClean->removeEntities();
  2. steinmb revised this gist May 16, 2020. 1 changed file with 13 additions and 3 deletions.
    16 changes: 13 additions & 3 deletions CleanUp.php
    Original file line number Diff line number Diff line change
    @@ -35,8 +35,18 @@ public function removeEntities()
    print 'Deleted ' . count($entities) . ' of type ' . $this->entityType . PHP_EOL;
    }

    public function summarise() {
    foreach (\Drupal::entityTypeManager()->getDefinitions() as $entityType) {
    $entityTypeToClean = new self($entityType->id());
    print count($entityTypeToClean->findEntities()) . "\t" . $entityType->id() . PHP_EOL;
    }
    }

    }

    // Example use. Find all terms and remove them.
    $entityTypeToClean = new CleanUp('taxonomy_term');
    $entityTypeToClean->removeEntities();
    // Example use.
    $entityType = 'node';
    $entityTypeToClean = new CleanUp($entityType);
    $entityTypeToClean->summarise();
    //print count($entityTypeToClean->findEntities()) . "\t" . $entityType . PHP_EOL;
    //Warning! $entityTypeToClean->removeEntities();
  3. steinmb created this gist May 16, 2020.
    42 changes: 42 additions & 0 deletions CleanUp.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    <?php

    final class CleanUp
    {
    private $entityType;
    private $storage;

    public function __construct(string $entityType)
    {
    $this->entityType = $entityType;
    $this->storage = \Drupal::entityTypeManager()->getStorage($this->entityType);
    }

    public function findEntities(): array
    {
    return \Drupal::entityQuery($this->entityType)
    ->execute();
    }

    private function load(): array
    {
    $fids = $this->findEntities();
    return $this->storage->loadMultiple($fids);
    }

    public function removeEntities()
    {
    $entities = $this->load();

    try {
    $this->storage->delete($entities);
    } catch (\Drupal\Core\Entity\EntityStorageException $e) {
    print 'Unable to delete ' . count($entities) . PHP_EOL;
    }
    print 'Deleted ' . count($entities) . ' of type ' . $this->entityType . PHP_EOL;
    }

    }

    // Example use. Find all terms and remove them.
    $entityTypeToClean = new CleanUp('taxonomy_term');
    $entityTypeToClean->removeEntities();