23 Aug.2019

Drupal 10: Remove all paragraphs entities from the site programmatically

drupal paragraphs

Sometimes we need to completely remove paragraphs entities, for example, when you copy a site for another project.

You can use custom code in your custom module or install devel_php and run with admin interface on /devel/php.

use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\Entity\Paragraph;
 
$ids = \Drupal::entityQuery('paragraph')->execute();
foreach ($ids as $id) {
  $paragraph = Paragraph::load($id);
  if ($paragraph instanceof ParagraphInterface) {
    $paragraph->delete();
  }
}

Remove all paragraphs on the site.

use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\Entity\Paragraph;
 
$ids = \Drupal::entityQuery('paragraph')->execute();
foreach ($ids as $id) {
  $paragraph = Paragraph::load($id);
  if ($paragraph instanceof ParagraphInterface && $paragraph->getOwnerId() == 0) {
    $paragraph->delete();
  }
}

Remove paragraphs created by an anonymous user only.

use Drupal\paragraphs\ParagraphInterface;
use Drupal\paragraphs\Entity\Paragraph;
 
$ids = \Drupal::entityQuery('paragraph')->execute();
foreach ($ids as $id) {
  $paragraph = Paragraph::load($id);
  if ($paragraph instanceof ParagraphInterface && $paragraph->bundle() == 'contact_us') {
    $paragraph->delete();
  }
}

Remove paragraphs by type (bundle).

Ruslan Piskarov

Ukraine
PHP/WEB Developer / Drupal Expert. More than 11 years of experience delivering Drupal based General Purpose solutions for different sectors such as Job Boards, Product Portfolios, Geo Coding, Real Estate solutions, E-Commerce, Classifieds, Corporate and online Magazines/Newspapers.