Skip to content

Instantly share code, notes, and snippets.

@steinmb
Last active May 16, 2020 10:10
Show Gist options
  • Save steinmb/3afe0138dbb4537e7618324c2ee2b730 to your computer and use it in GitHub Desktop.
Save steinmb/3afe0138dbb4537e7618324c2ee2b730 to your computer and use it in GitHub Desktop.
Helper script used during Drupal 7 to 8/9 migration. Check if there are any broken data to clean before running migrate:import. Delete all terms, files, nodes and so on. Use with care.
<?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;
}
}
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment