-
-
Save MatthieuScarset/5319e4c19a863f951d87b6c06014d6ec to your computer and use it in GitHub Desktop.
// 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(); | |
} |
I think you don't even have to loop over entities to delete them.
This should work too:
$paragraphs = $paragraph_storage->loadMultiple($results);
$paragraph_storage->delete($paragraphs);
Also - although I don't know what is more efficient - I prefer to use IN
statements rather than adding condition group to queries:
$query = \Drupal::entityQuery('paragraph')
->condition('type', ['grid', 'accordion'], 'IN')
...
@MatthieuScarset β Oh yeah, this is better indeed! I now see, you also have the multi-delete in the samples. π Thank you for reviewing! π
/**
* Remove all grid and accordion paragraphs.
*/
function MYMODULE_update_9001(&$sandbox) {
$paragraph_storage = \Drupal::entityTypeManager()->getStorage('paragraph');
$query = \Drupal::entityQuery('paragraph')
->condition('type', ['grid', 'accordion'], 'IN');
$results = $query->execute();
if (!empty($results)) {
if (!empty($paragraphs = $paragraph_storage->loadMultiple($results))) {
$paragraph_storage->delete($paragraphs);
}
}
}
The examples they show are very clear and well understood.
An additional question, if I have a content type Xy and in it I have a field of type Paragraph (Px), in what order should it be deleted?
1.- I eliminate the field of the TC Xy
2.- I eliminate all the paragraphs of type Px
3.- I eliminate the Paragraph type.
Programmatically, how to remove the field of type Paragraph from a content type?
Dumping for later.