Hi all. I need to purge a fairly large set of specific nodes (about 300) from a site that has 14,000+ overall, and am looking for advice on the best way to go about it.

Here's the situation:

  • This is a Drupal 5 site, and the nodes in question are all of a single CCK type
  • All the CCK fields used in that nodetype are unique, so all the field data resides in a "content_type_content_district" table
  • In addition to the NID, each node has a unique value in a field called "District ID" (field_district_id_value in the content_type_content_district table)
  • I have a list of the District ID's for the nodes that need to be deleted
  • Those District ID's are numbers, but they are not an uninterrupted series

My initial thought was that I'd just do this right at the database level -- something like:

DELETE FROM content_type_content_district
    WHERE field_district_id_value = [big long string of District ID's here]

But that wouldn't touch the node, node_revisions, or term_node tables, among others -- which at best would leave a bloated database, and probably would create all sorts of other problems for the site.

I guess I could do some crazy multi-table delete. Or use joins to tie the "content_type_content_district" to each of the others, one at a time, by NID, and delete all the related node info from them before doing a delete on that "content_type_content_district" table itself. Or run a query to pull a list of the NID's that match each of the District ID's, and then use that list of NID's somehow.

Any suggestions from those who've had to do similar bulk deletions? All of these approaches seem overly complicated -- I'm hoping there's an elegant way to tap into the node_delete function or something.

Thanks in advance for any help!

Comments

nicolash’s picture

Here's an example for nodes of type 'event'. You obviously need a different initial query to get the node IDs, but it seems that is not your concern.

Once you have that you loop through them and use node_delete...it will take care of any joins internally in Drupal.

$result = db_query("
    SELECT nid 
    FROM {node} 
    WHERE type = '%s'", 
    'event');

while ($node = db_fetch_object($result)) {
  node_delete($node->nid);
}

ourbrisbane.com

TKS’s picture

That was exactly what I needed. Just changed the initial query to:

$result = db_query("
    SELECT nid
    FROM {content_type_content_district}
    WHERE nid IN (**aforementioned big long string of District IDs, comma-separated**)");

Worked great. Thanks again!

seakayjay’s picture

How can I delete nodes with a specific term?