Delete all nodes from a particular content type using devel generate

Learn more

Master Drupal Development: Get the book to scale the learning curve faster

Start learning Drupal 8 development today: Get the free 7 lesson course

You’ll often find yourself having to delete all content for a particular content type. You may have tested imported content via feeds and you then need to delete what you’ve imported. Or content was created that is no longer required. Manually ticking everything in the content list is fine if you have just a handful of nodes, but if you have a lot of content to delete, it can get very time consuming.

There are a few bulk delete modules available, which will do the job. But if you already have devel installed (highly recommended for any sort of Drupal development), you don’t actually need to install anything else. Devel’s Devel Generate module can delete all content for a content type, or multiple content types. Devel Generate is designed to make it easier to create dummy content when building and testing new sites, and then delete that dummy content. But, even if Devel Generate didn’t create the content, it can still delete it.

Drush

The trick is to use the “kill” option . This is what tells devel to delete all content. Normally you’d use kill to delete all dummy nodes that Devel Generate created and then have devel generate a bunch of new dummy nodes. But if all you want to do is delete the nodes and not create any new content (perhaps you want to retest your feed's import), you can specify 0 as the number of new nodes to create.

  1. drush genc 0 —kill

And if you want to delete all nodes from a particular content type, just specify the content type:

  1. drush genc 0 —kill –types=article

And if you want to delete all nodes from multiple content types:

  1. drush genc 0 —kill –types=article, book

UI

You can also do this in the UI if you’d rather not use Drush. Here are the steps.

  1. Go to admin/config/development/generate/content
  2. Select the content type(s) you want to delete
  3. Tick - Delete all content in these content types before generating new content.
  4. How many nodes would you like to generate? - set 0
  5. Click generate

Drupal 7 vs Drupal 8

The process for deleting nodes using Devel generate on Drupal 8 is identical as Drupal 7 for both the Drush and UI method.

If you liked this, you'll love my book, Master Drupal Module Development.

"..the must have drupal developers book"


Feeling stuck with Drupal 8 module dev?

Get the free 7 lesson course that will help you get started today without feeling overwhelmed.

  • Create Drupal modules with just a few commands using the Drupal Console
  • Create custom pages
  • Create custom blocks
  • Create admin forms
  • Demystify routers and controllers
  • Bonus material

Find out more


Add new comment