Cloud Service >> Knowledgebase >> Drupal >> How to Delete All Nodes of a Given Content Type in Drupal?
submit query

Cut Hosting Costs! Submit Query Today!

How to Delete All Nodes of a Given Content Type in Drupal?

Deleting all nodes of a specific content type in Drupal can be necessary for various reasons, such as: 

 

- Cleaning up old content

- Making way for new data structures

- Simply performing maintenance tasks

 

There are several ways to achieve this, from manual deletion through the Drupal UI to writing custom scripts. Below, we have outlined several methods to delete all nodes of a given content type in Drupal, highlighting both user-friendly and programmatic approaches.

1. Using the Drupal Admin Interface

The Drupal Admin interface provides an easy way to delete nodes for those who prefer a user-friendly approach and have a manageable number of nodes.

Navigate to Content Management

- Go to the Drupal admin dashboard.

- Click "Content" in the admin menu (/admin/content).

 

Filter by Content Type

 

- Use the filters at the top to select the specific content type you want to delete.

- This will list all nodes of the selected content type.

 

Select Nodes to Delete

- Use the checkboxes to select all nodes on the current page.

- If you have multiple pages of nodes, repeat this step for each page or adjust the items per page setting to include all nodes at once.

 

Bulk Operations

- In the "Action" dropdown, select "Delete selected content."

- Click "Apply" and confirm the deletion.

 

This method is straightforward but may not be practical for many nodes due to pagination and manual selection.

2. Using Views Bulk Operations (VBO)

The Views Bulk Operations (VBO) module can be very helpful for more extensive or complex node deletion tasks.

Install and Enable VBO

- Download and enable the Views Bulk Operations module (drush en views_bulk_operations).

 

Create a View

- Go to admin/structure/views/add.

- Create a new view of "Content" and add a page display.

- In the format section, select "Table" and then "Bulk operations: Content."

Filter by Content Type

- Add a filter criterion for "Content type" and select the desired content type.

Add Bulk Delete Operation

- In the VBO settings, add the "Delete content" operation.

- Save the view and navigate to it.

Execute the Operation

- Select all nodes using the bulk operations checkbox.

- Choose "Delete content" from the operations dropdown and apply it.

VBO is powerful and can handle large datasets, making it suitable for mass deletion tasks.

3. Using Drush

Drush (Drupal Shell) provides a quick and efficient way to delete nodes for developers and those comfortable with command-line tools.

Install Drush

- Ensure Drush is installed and configured for your Drupal site.

Identify Node IDs

Use a SQL query to fetch all node IDs of the desired content type:

SELECT nid FROM node WHERE type = 'your_content_type';

Delete Nodes

Use Drush to delete nodes by ID:

drush eval "node_delete_multiple(array(1, 2, 3));"

Alternatively, if you have many nodes, write a script to automate this:

nids=$(drush sql-query "SELECT nid FROM node WHERE type='your_content_type'")

for nid in $nids; do

drush eval "node_delete($nid);"

done

Drush is extremely powerful for batch operations but requires comfort with the command line.

4. Writing a Custom Script

You can write a custom script in PHP for complete control or complex conditions.

Prepare Your Environment

- Ensure you have a local development environment set up or access to your Drupal site's codebase.

Write the Script

Create a PHP script with the following content:

use Drupal\node\Entity\Node;

// Load all node IDs of a specific content type.

$nids = \Drupal::entityQuery('node')

->condition('type', 'your_content_type')

->execute();

// Load and delete nodes.

$nodes = Node::loadMultiple($nids);

foreach ($nodes as $node) {

$node->delete();

}

print "Deleted " . count($nids) . " nodes.";

Execute the Script

Deleting all nodes of a specific content type in Drupal can be necessary for various reasons, such as: 

 

- Cleaning up old content

- Making way for new data structures

- Simply performing maintenance tasks

 

There are several ways to achieve this, from manual deletion through the Drupal UI to writing custom scripts. Below, we have outlined several methods to delete all nodes of a given content type in Drupal, highlighting both user-friendly and programmatic approaches.

1. Using the Drupal Admin Interface

The Drupal Admin interface provides an easy way to delete nodes for those who prefer a user-friendly approach and have a manageable number of nodes.

Navigate to Content Management

  - Go to the Drupal admin dashboard.

- Click "Content" in the admin menu (/admin/content).

 

Filter by Content Type

  - Use the filters at the top to select the specific content type you want to delete.

- This will list all nodes of the selected content type.

Select Nodes to Delete

   - Use the checkboxes to select all nodes on the current page.

- If you have multiple pages of nodes, repeat this step for each page or adjust the items per page setting to include all nodes at once.

Bulk Operations

   - In the "Action" dropdown, select "Delete selected content."

- Click "Apply" and confirm the deletion.

 

This method is straightforward but may not be practical for many nodes due to pagination and manual selection.

2. Using Views Bulk Operations (VBO)

The Views Bulk Operations (VBO) module can be very helpful for more extensive or complex node deletion tasks.

Install and Enable VBO

- Download and enable the Views Bulk Operations module (drush en views_bulk_operations).

Create a View

  - Go to admin/structure/views/add.

- Create a new view of "Content" and add a page display.

- In the format section, select "Table" and then "Bulk operations: Content."

 

Filter by Content Type

  - Add a filter criterion for "Content type" and select the desired content type.

Add Bulk Delete Operation

  - In the VBO settings, add the "Delete content" operation.

- Save the view and navigate to it.

Execute the Operation

  - Select all nodes using the bulk operations checkbox.

- Choose "Delete content" from the operations dropdown and apply it.

VBO is powerful and can handle large datasets, making it suitable for mass deletion tasks.

3. Using Drush

Drush (Drupal Shell) provides a quick and efficient way to delete nodes for developers and those comfortable with command-line tools.

Install Drush

- Ensure Drush is installed and configured for your Drupal site.

Identify Node IDs

Use a SQL query to fetch all node IDs of the desired content type:

SELECT nid FROM node WHERE type = 'your_content_type';

Delete Nodes

Use Drush to delete nodes by ID:

drush eval "node_delete_multiple(array(1, 2, 3));"

Alternatively, if you have many nodes, write a script to automate this:

nids=$(drush sql-query "SELECT nid FROM node WHERE type='your_content_type'")

for nid in $nids; do

drush eval "node_delete($nid);"

done

Drush is extremely powerful for batch operations but requires comfort with the command line.

4. Writing a Custom Script

You can write a custom script in PHP for complete control or complex conditions.

Prepare Your Environment

- Ensure you have a local development environment set up or access to your Drupal site's codebase.

 

Write the Script

 

Create a PHP script with the following content:

use Drupal\node\Entity\Node;

// Load all node IDs of a specific content type.

$nids = \Drupal::entityQuery('node')

->condition('type', 'your_content_type')

->execute();

// Load and delete nodes.

$nodes = Node::loadMultiple($nids);

foreach ($nodes as $node) {

$node->delete();

}

print "Deleted " . count($nids) . " nodes.";

Execute the Script

- Run the script in a drush environment or as part of a custom module.

This method offers flexibility and can be tailored to specific needs, but it requires a good understanding of Drupal's API and PHP.


To Sum it Up!

Deleting all nodes of a given content type in Drupal can be achieved through various methods, each suited to different levels of expertise and requirements. The Drupal Admin Interface and Views Bulk Operations module are user-friendly options for non-developers, while Drush and custom scripts offer powerful and flexible solutions for developers. Choosing the right method depends on the number of nodes, your familiarity with Drupal tools, and the specific requirements of your task.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!