Delete hundreds of nodes by ID programmatically 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
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME
IN (
'entity_id'
)
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) {
  node_delete($n);
}