node_delete() is not deleting nodes
Question
I'm trying to use a drush function to flush out all of the drealty module's nodes in my drupal 6 install. However, something is causing a memory leak and the nodes are not being deleted after node_delete is called. What can I do?
Here is the memory exhausted error:
drush rets-flush
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) in /home/directory/public_html/sites/all/modules/filefield/field_file.inc on line 300
Drush command terminated abnormally due to an unrecoverable error. [error]
Error: Allowed memory size of 134217728 bytes exhausted (tried to
allocate 71 bytes) in
/home/directory/public_html/sites/all/modules/filefield/field_file.inc,
line 300
Here is the drush rets-flush function:
function drush_drealty_rets_flush() {
set_time_limit(0);
$result = db_query("SELECT nid FROM {node} WHERE type = 'drealty_property' OR type = 'drealty_agent' OR type = 'drealty_office' OR type = 'drealty_open_house'", $conid);
$count = 0;
while ($nid = db_fetch_object($result)) {
node_delete($nid->nid);
drush_log(dt('Deleting node id: @nid. ', array('@nid' => $nid->nid)));
$count++;
}
drush_log(dt('Deleted @count nodes', array('@count' => $count)), 'success');
}
There are about 5000 records total.
Solution
You may find that you still run out of memory if you have enough nodes of the drealty type. This is because node_delete()
calls node_load()
, and that adds each node to a static cache. If you run into this, simply call node_load(NULL, NULL, TRUE)
once every 50 - 100 iterations.
OTHER TIPS
Found the answer myself. I had to run drush as admin:
drush -u admin rets-flush
I thought drush ran as admin automatically, but I guess not.