$nids = db_query('SELECT nid FROM node WHERE NOT EXISTS(SELECT NULL FROM users WHERE users.uid = node.uid)')->fetchCol(); foreach($nids as $nid) { $node = node_load($nid); $node->uid = 0; node_save($node); }