Last active
May 16, 2020 10:10
-
-
Save steinmb/3afe0138dbb4537e7618324c2ee2b730 to your computer and use it in GitHub Desktop.
Revisions
-
steinmb revised this gist
May 16, 2020 . 1 changed file with 13 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } } final class Utils { private function __construct() {} static function summarise() { foreach (\Drupal::entityTypeManager()->getDefinitions() as $entityType) { $entityTypeToClean = new CleanUp($entityType->id()); print count($entityTypeToClean->findEntities()) . "\t" . $entityType->id() . PHP_EOL; } } } // Example use. Utils::summarise(); $entityType = 'file'; $entityTypeToClean = new CleanUp($entityType); print 'Found ' . count($entityTypeToClean->findEntities()) . ' ' . $entityType . '(s)' . PHP_EOL; //Danger zone! $entityTypeToClean->removeEntities(); -
steinmb revised this gist
May 16, 2020 . 1 changed file with 13 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. $entityType = 'node'; $entityTypeToClean = new CleanUp($entityType); $entityTypeToClean->summarise(); //print count($entityTypeToClean->findEntities()) . "\t" . $entityType . PHP_EOL; //Warning! $entityTypeToClean->removeEntities(); -
steinmb created this gist
May 16, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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();