Skip to content

Instantly share code, notes, and snippets.

@Jaesin
Created June 22, 2015 07:14
Show Gist options
  • Save Jaesin/b4e6b8331671e231b9e8 to your computer and use it in GitHub Desktop.
Save Jaesin/b4e6b8331671e231b9e8 to your computer and use it in GitHub Desktop.
Delete all content entities in Drupal 8.
<?php
// Delete all nodes.
entity_delete_multiple('node', \Drupal::entityQuery('node')->execute());
// Delete all files.
entity_delete_multiple('file', \Drupal::entityQuery('file')->execute());
// Delete all taxonomy terms.
entity_delete_multiple('taxonomy_term', \Drupal::entityQuery('taxonomy_term')->execute());
// Delete all block content.
entity_delete_multiple('block_content', \Drupal::entityQuery('block_content')->execute());
// Delete all menu links.
entity_delete_multiple('menu_link_content', \Drupal::entityQuery('menu_link_content')->execute());
// Delete all users except 1 and 0.
entity_delete_multiple('user', \Drupal::entityQuery('user')->condition('uid', '1', '!=')->condition('uid', '0', '!=')->execute());
@divined
Copy link

divined commented Sep 5, 2017

$ids = \Drupal::entityQuery('node')->execute(); -> can be removed

$controller = \Drupal::entityTypeManager()->getStorage('entity_type');
$entities = $controller->loadMultiple(); -> load all entities, if no $ids
$controller->delete($entities);

@arunkumarkuppuswamy
Copy link

For deleting the comment

entity_delete_multiple('comment', \Drupal::entityQuery('comment')->execute());

@syntheticminds
Copy link

This is also a great way to increase the performance of your Drupal site.

@BERRAMOU
Copy link

BERRAMOU commented May 7, 2019

For deleting custom entities:

$storage_handler = \Drupal::entityTypeManager()->getStorage('ENTITY_ID');
$entities = $storage_handler->loadMultiple($ids);
$storage_handler->delete($entities);

@leymannx
Copy link

leymannx commented Dec 7, 2021

@Jaesin – Can you maybe update the Gist? Because entity_delete_multiple is deprecated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment