Drush command to delete content going out of memeory. What can I do? I have implemented drush hooks to build a drush command that deletes a lot of legacy nodes. It basically does a node_delete on a lot of nodes in a loop. However, I have the memory limit set to ~380MiB in settings.php and when I run the command to delete large number of nodes, say 100 000, drush terminates abnormally with an error indicating that it ran out of memeory. This happens by the time it deletes 20000 nodes. I have looked around and checked the definition of node_delete. In the definition there is a comment about the possibility of going out of memory.    

I tried unsetting variables, and I tried doing node_load(NULL,NULL,TRUE)-- as suggested one drupaler on this forum-- hoping to release some memory every number of iterations but this did not work. So is there anything I can do? I am trying to avoid breaking it into chunks for ease of release.
If you look at the code for node_delete_multiple() it does a node_load_multiple().  While this seems strange, it has to do this so that it can invoke all of the proper hooks so that other modules have the full $node object in order to do their thing (eg, delete the attached field data).

Because Drupal uses aggressive static caching to avoid redundant database accesses, memory usage can swell.  This can't be freed or disabled.  If this becomes an issue, you can do temp edits to the EntityControllers to comment out the static caches.  You could also make a temp edit to drupal_static() to effectively disable it.

I would come up with a good EntityFieldQuery to get the list of nodes to delete.  This will just give you back the nids.

Suck out the nids, and trim the array to some limit.

Call node_delete_multiple() on the result.

$query = new EntityFieldQuery();
$entities = $query-&gt;entityCondition('entity_type', 'node', '=')
  -&gt;entityCondition('bundle', $bundle)
  -&gt;addCondition(...)
  -&gt;execute();

$nids = array_slice($entities['node'], 0, 1000);

print 'Deleting ' . count($nids) . ' nodes ... ';

if (!empty($nids)) {
  node_delete_multiple($nids);
}

print "done\n";


Put this in a standalone PHP and execute is from drush, eg

drush -u 1 scr nuke_everything.php


Run it until everything is gone.

The above solution is for Drupal 7, but the approach is essentially the same for Drupal 6.  You would do a db_query() to get the nids, and then loop through and call node_delete() on each.  However, Drupal 6 also does a full node_load(), too.  In this instance, it does bypass its static cache, but this doesn't mean that modules aren't also doing any static caching.  For Drupal 6, you would need to chase all of the possible hook_nodeapi('delete') and look for static caching and static caching in any functions that get called, and do temp edits.