Last active
August 28, 2024 05:55
-
-
Save MatthieuScarset/5319e4c19a863f951d87b6c06014d6ec 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(); | |
} |
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?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@MatthieuScarset β Oh yeah, this is better indeed! I now see, you also have the multi-delete in the samples. π Thank you for reviewing! π