Get 69% Off on Cloud Hosting : Claim Your Offer Now!
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.
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.
- Go to the Drupal admin dashboard.
- Click "Content" in the admin menu (/admin/content).
- 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.
- 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.
- 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.
The Views Bulk Operations (VBO) module can be very helpful for more extensive or complex node deletion tasks.
- Download and enable the Views Bulk Operations module (drush en views_bulk_operations).
- 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."
- Add a filter criterion for "Content type" and select the desired content type.
- In the VBO settings, add the "Delete content" operation.
- Save the view and navigate to it.
- 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.
Drush (Drupal Shell) provides a quick and efficient way to delete nodes for developers and those comfortable with command-line tools.
- Ensure Drush is installed and configured for your Drupal site.
Use a SQL query to fetch all node IDs of the desired content type:
SELECT nid FROM node WHERE type = 'your_content_type';
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.
You can write a custom script in PHP for complete control or complex conditions.
- Ensure you have a local development environment set up or access to your Drupal site's codebase.
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.";
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.
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.
- Go to the Drupal admin dashboard.
- Click "Content" in the admin menu (/admin/content).
- 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.
- 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.
- 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.
The Views Bulk Operations (VBO) module can be very helpful for more extensive or complex node deletion tasks.
- Download and enable the Views Bulk Operations module (drush en views_bulk_operations).
- 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."
- Add a filter criterion for "Content type" and select the desired content type.
- In the VBO settings, add the "Delete content" operation.
- Save the view and navigate to it.
- 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.
Drush (Drupal Shell) provides a quick and efficient way to delete nodes for developers and those comfortable with command-line tools.
- Ensure Drush is installed and configured for your Drupal site.
Use a SQL query to fetch all node IDs of the desired content type:
SELECT nid FROM node WHERE type = 'your_content_type';
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.
You can write a custom script in PHP for complete control or complex conditions.
- Ensure you have a local development environment set up or access to your Drupal site's codebase.
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.";
- 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.
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.
Let’s talk about the future, and make it happen!
By continuing to use and navigate this website, you are agreeing to the use of cookies.
Find out more