Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MatthieuScarset/5319e4c19a863f951d87b6c06014d6ec to your computer and use it in GitHub Desktop.
Save MatthieuScarset/5319e4c19a863f951d87b6c06014d6ec to your computer and use it in GitHub Desktop.
Programatically delete content in Drupal 8
// 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();
}
@leymannx
Copy link

leymannx commented Nov 9, 2021

Dumping for later.

/**
 * Remove all grid and accordion paragraphs.
 */
function MYMODULE_update_9001(&$sandbox) {
  $paragraph_storage = \Drupal::entityTypeManager()->getStorage('paragraph');
  $query = \Drupal::entityQuery('paragraph');
  $or_group = $query->orConditionGroup()
    ->condition('type', 'grid')
    ->condition('type', 'accordion');
  $query->condition($or_group);
  $results = $query->execute();
  if (!empty($results)) {
    if (!empty($paragraphs = $paragraph_storage->loadMultiple($results))) {
      foreach ($paragraphs as $paragraph) {
        $paragraph->delete();
      }
    }
  }
}

@MatthieuScarset
Copy link
Author

MatthieuScarset commented Nov 11, 2021

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')
...

@leymannx
Copy link

leymannx commented Nov 11, 2021

@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);
    }
  }
}

@Gonzalo2683
Copy link

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