How to Bulk Delete Content in Drupal

This tutorial will show you the various ways of batch deleting content inside Drupal. You may want to delete content if you’re doing the following:

  • You want to work on a clone of a Drupal site but delete some or all of its content.
  • You want to clean up your Drupal database and delete some old content
  • You just want to batch delete content for any other reason.

There are different ways this can be achieved. We will cover the following ways:

Which method should you use to batch delete content in Drupal?

This is a great question. And the answer is limited to how many nodes you want to delete, your PHP memory_limit and the amount of time you have. We have simplified the answer to this question in the following table. We have used approximations as there is no clear cut answer.

Method

Number of nodes

Time Required

Limitations

Drupal Core UI

< 300

5-10 mins

Manual process is done in batches of 50. Possibility of timing out if you have complex field/paragraph configurations and big file uploads and a small PHP memory_limit

Drush

unlimited

few seconds

PHP memory_limit

VBO

unlimited

5-10 mins

PHP memory_limit, batch size, setting up VBO

Using Drupal Core UI

You should use this method if you have a small Drupal site and around less than 300 nodes to delete. This is because using Drupal core’s UI allows you to delete only 50 nodes at a time by default. This becomes tedious as your site is bigger.

Steps

Step 1: Go to /admin/content

Step 2:  Check the boxes of the nodes you want to delete or click the box at the top to select all as shown in Figure 1.

How to bulk delete content in admin screen.
Figure 1

Step 3: Select the Action “Delete Content” and then click on “Apply to selected items”.

Limitations

Using this method has the limitation of batch deleting a maximum of 50 nodes (can be increased if you edit the view). You also run the possibility of timing out if you have complex field configurations with nested paragraphs coupled with big file uploads and a small PHP memory_limit.

Using Drush

Using the command line, you can use Drush and this is the recommended way. You can customize what nodes you want to batch delete so let’s see the commands and their options.

Command: drush entity:delete –help

Here is the output.

As you see, you can specify various parameters such as by bundle type, nodes ids etc.

Common Commands

Delete all nodes of article content type: drush entity:delete node --bundle=article

Delete all nodes: drush entity:delete node

Delete all files: drush entity:delete file

Delete specific node ids: drush entity:delete node 515,524,511

Delete all nodes except specific ids: drush entity:delete node –exclude=515,524,511

If you have a very large site and you are experiencing timeouts, you can batch delete in chunks and specify the chunk/batch size: drush entity:delete node --chunks=100

Limitations

Using Drush is the most limitless and fastest way to batch delete content. However, you are ultimately limited by your PHP memory_limit and your knowledge of Drush.

Using Views Bulk Operations (VBO)

View our tutorial here on how to use Views Bulk Operations in Drupal.

VBO allows you to batch delete content and also, most importantly, you can set batch size to avoid timeouts.

Here is our VBO page:

In the field configuration settings of the VBO field, you can specify the batch size:

If you are experiencing timeouts, try setting a smaller batch size.

Limitations

The limitation is that you have first to install and configure VBO. This is not a major task but it is still an extra step you have to do nonetheless. After that, you are limited by your PHP memory_limit and batch size configuration.

Conclusion

Drupal developers are often tasked with batch deleting content, especially in their local environment. There are a few ways that you can batch delete content within Drupal. In this tutorial, we have demonstrated the top three ways and provided the limitations of each one.

We have recommended that you use Drush as this option offers the most flexibility and is the least time-consuming.

Leave a Comment

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.