23
Aug.2019
Drupal 11: Remove all paragraphs entities from the site programmatically
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')->accessCheck(FALSE)->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')->accessCheck(FALSE)->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')->accessCheck(FALSE)->execute(); foreach ($ids as $id) { $paragraph = Paragraph::load($id); if ($paragraph instanceof ParagraphInterface && $paragraph->bundle() == 'contact_us') { $paragraph->delete(); } }
Remove paragraphs by type (bundle).
Comments