Developing a content management system requires a large amount of content to be created. Every developer who has had the opportunity to work on these types of projects would understand the pain of having to create sample contents only to be told that a new feature needs to be included. Features like content moderation, translation works even with existing content but not all the time. So we are left with hundreds of contents that we need to delete. It’s fine if you’re developing for a project with manually created content, but what about those that use CSV import or worst fetched information from Product Information Management(PIM) Systems? This leaves us with thousands of products and deleting them from the content page (/admin/content) would take forever.

 

In this article we will discuss the different methods we use for mass deleting contents and when do we use them.

 

  1. Truncate Database Tables
    1. Requirements
      1. Database Knowledge
      2. Database/database server access
    2. How to Delete
      1. Before starting to gather anything, we would like to iterate that this is the least preferred method. 
      2. The first thing you need to do is identify the content types you want contents to be deleted then you need to list all the field machine names. 
      3. After all of that, you can now connect to your database and start truncating the node_xx tables that match your field machine names. 
      4. You will also need to truncate the revisions specially when you have content moderation in place.

 

For terminal or cmd command

TRUNCATE TABLE `your_db_name`.`node__field_category_description`;

 

For MySQL Workbench 

 

  1. When to Use this Method

As I have said above, this is the least preferred method and the reason is it will require you to be attentive to what database table you are truncating. Another important thing to take note of is that this doesn’t work for all set up. You have to consider other none node tables if you have entity references to that content type like paragraphs for example. Again, leave this for as a last resort and instead try the next methods first.

 

  1. Creating a Views Page
    1. Requirements
      1. Views UI core module is enabled (enabled by default)
      2. Administrator permission (Assumed)
    2. How to Create
      1. First you go to the views management page (/admin/structure/views) and click on “Add Views”. 
      2. After filling out the views basic information, select “content” in the views settings and select the appropriate content type/s that you want included in the views page. 
      3. Make sure to tick the “Create a Page” in the page settings and fill out the necessary information like the preferred url but the most important setting is to choose “Table” instead of unformatted list. 
      4. Click “Save and Edit” and you will be redirected to the views configuration page.
      5. Here you will only see the title added to the list of fields so lets click “Add” to add a field and search for “node operations” in the modal window
      6. Select “node operations bulk forms”, add and configure accordingly but default is fine for this cause.
      7. Save the views page and visit using the actual URL and now you can bulk delete the contents you want

 

  1. When to Use this Method

You probably already know that this functionality is already available in the default content page so why bother creating a new one, right? The simple answer is, we want the page to load faster so we have to strip the other information that we don’t need like: published or unpublished, author, etc. And because of this we can choose to display more items so we can delete more at a time. We also do not advise reconfiguring the default content page since it is already optimized for its own purpose and that is to display contents. We always use this method whenever we have a huge amount of data to manage and installing modules is no longer an option.

 

  1. Devel Generate
    1. Requirements
      1. Devel Module
    2. How to Delete
      1. Download the devel module to your Drupal project and install devel and devel generate
      2. After installing just navigate to /admin/config/development/generate/content
      3. Select the content types of which contents you want to delete
      4. Tick the checkbox that says “Delete all Content …” as shown in the sample image
      5. Put 0 in the field for “How many contents to generate” and that’s it

 

  1. When to Use this Method

We usually use this method while testing on the dev environment because we always have devel installed. The Devel module is really very handy when developing and we make sure we have it on every project. Although, to minimize risk we do not deploy it to the Production environment specially when our client wants to have access to the admin panel. It’s too risky and unnecessary most of the time. That’s why we suggest using this in an environment where you can install the Devel module.

 

  1. The Drush Way
    1. Requirements
      1. Drupal 9
      2. Drush 9
      3. Access to drush console
    2. How to Delete
      1. Open drush console and type the following command and wait for the process to complete

 

drush entity:delete node --bundle=<your_content_type_machine_name>

  1. When to Use this Method

We just discovered this method recently while working with a Drupal 9 project and we found it very handy. We have been using Devel but in our case we needed to delete mistakenly created nodes from the Production environment where we can’t install any more modules. We suggest using this the most whenever possible since it does not require any additional modules.