Forked from MatthieuScarset/drupal8_programatically_delete_content.php
Created
June 8, 2021 08:26
-
-
Save brain2xml/94517b9d5ed2f0ee8cac74e9cecbf9ff to your computer and use it in GitHub Desktop.
Programatically delete content in Drupal 8
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 characters
// Enable Devel module and go to /devel/php | |
$nodes = \Drupal::entityQuery("node") | |
->condition('created', strtotime('-30 days'), '<=') | |
->execute(); | |
$storage_handler = \Drupal::entityTypeManager()->getStorage("node"); | |
// $entities = $storage_handler->loadMultiple(); // Delete ALL nodes. | |
$entities = $storage_handler->loadMultiple($nodes); | |
$storage_handler->delete($entities); | |
// Delete Taxonomy Term by Vocabulary | |
$controller = \Drupal::entityTypeManager()->getStorage('taxonomy_term'); | |
$all_terms = \Drupal::entityQuery("taxonomy_term")->condition('vid', 'document_type')->execute(); | |
foreach ($all_terms as $tid) { | |
$term = $controller->load($tid); | |
$controller->delete([$term]); | |
} | |
// Delete media | |
$medias = \Drupal::entityQuery("media") | |
->condition('created', strtotime('-30 days'), '<=') | |
->execute(); | |
$storage_handler = \Drupal::entityTypeManager()->getStorage("media"); | |
// $entities = $storage_handler->loadMultiple(); // Delete ALL medias. | |
$entities = $storage_handler->loadMultiple($medias); | |
$storage_handler->delete($entities); | |
// Delete files. | |
$medias = \Drupal::entityQuery("file") | |
->condition('created', strtotime('-30 days'), '<=') | |
->execute(); | |
$storage_handler = \Drupal::entityTypeManager()->getStorage("file"); | |
// $entities = $storage_handler->loadMultiple(); // Delete ALL files. | |
$entities = $storage_handler->loadMultiple($files); | |
$storage_handler->delete($entities); | |
// Delete all paragraphs by Type. | |
$paragraphs = \Drupal::entityTypeManager() | |
->getStorage('paragraph') | |
->loadByProperties(array('type' => 'paragraph_type')); // System name | |
foreach ($paragraphs as $paragraph) { | |
$paragraph->delete(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment