Unable to loop through all nodes in a content type and load them without hitting memory limits I'm having an issue in Drupal 8 where I am unable to loop through a large amount of nodes without hitting memory limits. The products content type in the below example has ~6000 nodes. The memory limit is currently set to 512M and is not changeable.

I've tried unsetting the $node variable and clearing the entity cache  in the loop without any success.

$query = \Drupal::entityQuery('node')
         -&gt;condition('type', 'product')
         -&gt;execute();
$nids = array_keys($query);

foreach ($nids as $nid) {
    $node = \Drupal\node\Entity\Node::load($nid);
    print $node-&gt;title-&gt;value;
}


The goal is to be able to iterate through all nodes of a content type without hitting memory limits.
I was able to get this to work by using the following command pulled from the deprecated node_load() function.

\Drupal::entityManager()-&gt;getStorage('node')-&gt;resetCache(array('NID'));

The fixed script would be:

$query = \Drupal::entityQuery('node')
         -&gt;condition('type', 'product')
         -&gt;execute();
$nids = array_keys($query);

foreach ($nids as $nid) {
    $node = \Drupal\node\Entity\Node::load($nid);
    print $node-&gt;title-&gt;value;
    \Drupal::entityManager()-&gt;getStorage('node')-&gt;resetCache(array($nid));
}