Out of memory whilst running CLI tasks

Many of Craft's CLI commands can suffer from memory leaks when operating over a large number of objects. One way to prevent this from occurring is to run the commands in small batches, resetting the memory usage between each batch.

Luckily Craft provides --limit and --offset parameters for many of its CLI commands. We can use these to process a small number of objects at a time.

Doing this manually could take a long time, so we like to use a little bit of bash scripting to automate the process for us:

c=0; while [[ c -lt 45000 ]]; do ./craft resave/orders --update-search-index=1 --offset=$c --limit=500; ((c = $c + 500)); done

You just need to:

  1. replace the 45000 with a number slightly higher than the total number of objects you expect to be included in the process
  2. replace the 500s with the size of each batch. This number will depend on how quickly you're hitting memory issues. Lower will reset the memory usage more often.
  3. replace ./craft resave/orders --update-search-index=1 with whatever command you'd like to run

Once you have your bash script ready to go you can start a Shell session from the Servd dashboard and paste it in!