In this article we will discuss the different methods we use for mass deleting contents and when do we use them.
Truncate Database Tables
Requirements
・Database Knowledge
・Database/database server access
How to Delete
- Before starting to gather anything, we would like to iterate that this is the least preferred method.
- 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.
- After all of that, you can now connect to your database and start truncating the node_xx tables that match your field machine names.
- 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
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.
Creating a Views Page
Requirements
Views UI core module is enabled (enabled by default)
Administrator permission (Assumed)
How to Create
- First you go to the views management page (/admin/structure/views) and click on “Add Views”.
- 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.
- 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.
- Click “Save and Edit” and you will be redirected to the views configuration page.
- 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
- Select “node operations bulk forms”, add and configure accordingly but default is fine for this cause.
- Save the views page and visit using the actual URL and now you can bulk delete the contents you want
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.
Devel Generate
Requirements
How to Delete
- Download the devel module to your Drupal project and install devel and devel generate
- After installing just navigate to /admin/config/development/generate/content
- Select the content types of which contents you want to delete
- Tick the checkbox that says “Delete all Content …” as shown in the sample image
- Put 0 in the field for “How many contents to generate” and that’s it
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.
The Drush Way
Requirements
Drupal 9
Drush 9
Access to drush console
How to Delete
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>
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.