I am trying to remove 700 or so nodes from my Drupal 7 system (they are duplicates) and have a big list of node IDs in a text file.

Have tried using Views Bulk Operations (VBO) but it doesn't allow me to enter a list of IDs.

Also looked at using Drush documentation, but it doesn't seem to have a node-delete command? (surely a pretty standard thing?)

So now I'm thinking of writing my own Python script to do the following:

  • Take list of input IDs (via input file)

(note: this is just pseudo code)

# remove nodes
Foreach id in file_in_ids:
   SQL(DELETE FROM node WHERE nid={id});

# clean up all rows in tables containing col 'entity_id':
tables_with_entity_id = SQL(SELECT DISTINCT TABLE_NAME
IN (
AND TABLE_SCHEMA = 'my_drupal_db')

foreach table_name in tables_with_entity_id:
  foreach id in file_in_ids:
    SQL(DELETE FROM {table_name} WHERE entity_id=id)

Does this seem like a sound approach?


You want to avoid deleting directly from the database. The node system distributes its data across a number of tables. Generally you always want to use the API. The method your after is node_delete() which takes the nid as its single argument. This will handle deleting all the associated data and firing off any hooks that are attached to the event.

An alternative approach:

  • Download and install the "Devel" module. This then gives you access to a page at /devel/php where you can run arbitrary PHP code within Drupal.

  • You could write custom code to load the text file and explode the nid's into an array. Something like this:

    $nids = file_get_contents("/path/to/file"); 
    $nids = explode(",", $nids); // This will depend on how your nids are delimited. Could be line break "\n"
    foreach ($nids as $n) {
|improve this answer|||||
  • You want to make sure you run them through node_delete so that all of the hooks fire. This is a very simple example, the code might need some improving. – Tim Aug 2 '13 at 1:08
  • 2
    Drupal 7 also has node_delete_multiple() – mpdonadio Aug 2 '13 at 1:31
  • Thanks a lot. Rather than install devel module and run node_delete() through /devel/php (I think it will time out, its a lot of nid's i'm gonna feed it), can I create a cli php script and call node_delete() within it (exactly like your code above)? – gvanto Aug 2 '13 at 1:36
  • I mean this is why I'm so surprised that drush does not faciliate node-deletion? – gvanto Aug 2 '13 at 1:38
  • Think I'll give this a go: zyxware.com/articles/1340/… – gvanto Aug 2 '13 at 1:42

Use Drush Entity extension. It implements several operations on entities via drush. One of them is entity-delete:

drush entity-delete node 1 2 3 4
|improve this answer|||||

FWIW, here's a similar menu callback function I wrote for another developer's site which had an issue with orphaned nodes being created without a node type set:

Hat tip to MPD - I wasn't aware of node_delete_multiple() for D7 (but this was written for a D6 site anyway)

    function es_node_cleanup_callback() {
      // get all nodes without node type
      $q = 'select nid from {node} where type = ""';
      $r = db_query($q);
      if(!$r) {
        drupal_set_message('query failed');
      else {
        $counter = 0;
        $message = 0;
        if(mysql_num_rows($r) > 0) {
          while($row = mysql_fetch_row($r)) {
            if(isset($row[0]) && ctype_digit($row[0])) {
              $counter ++;
            else if($message == 0) {
              drupal_set_message(print_r($row, 1));
              $message = 1;
          drupal_set_message($counter . ' nodes deleted');    
        else {
          drupal_set_message('No orphanned nodes');    

|improve this answer|||||
  • This could be altered to use the data from the text file via file_get_contents() and then using explode() to get the nids as an array, which could then either be passed to node_delete_multiple() or iterated through... – Paul Bennett Aug 2 '13 at 3:55

If they are duplicates they will have the same title, so you can views bulk operation , modify the view to search by title and delete (select all, including all nodes not in current page matching the search). Very useful.

You'd probably could also tweak the view to add node id's...

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.