I have hundreds of thousands of models in a folder. In my script I have a loop which loads each model at a time as object in my scene then render an image of it then delete the object. After couple of thousands models, blender consumed all my machine memory and then froze and stopped processing. Do I have to load the startup file every time before loading and processing each of the models to prevent this from happening? Or do I use the garbage collector? I'm not sure how I can avoid this from happening?

  • 1
    $\begingroup$ Yes, even when removing the object with bpy.ops.object.delete() blender maintains the data block: blender.stackexchange.com/a/27235/31447 $\endgroup$
    – brockmann
    Mar 10, 2017 at 13:40
  • $\begingroup$ post your script, does it have a lot of operators?, could it be better done with a batch render? $\endgroup$
    – batFINGER
    Mar 10, 2017 at 15:09
  • $\begingroup$ @batFINGER I've updated my question and included the script. Not sure if I can run it without opening blender and I'm not sure about batch render as I never used it before. If you could please advise. $\endgroup$
    – Tak
    Mar 10, 2017 at 23:36

2 Answers 2


I was able to avoid this by saving my file as a startup file then reloading the startup .blend to clean up all the data blocks.

  • 1
    $\begingroup$ Why not just save and reload the file? $\endgroup$
    – p2or
    Mar 14, 2017 at 10:01
  • $\begingroup$ @poor well, that's a good idea too :) do you want me to update my answer and add your way or you want to do it? $\endgroup$
    – Tak
    Mar 14, 2017 at 10:04
  • $\begingroup$ Great! I'm not sure if there is a better way, still testing... $\endgroup$
    – p2or
    Mar 14, 2017 at 10:18
  • $\begingroup$ @poor no worries, thank you very much. Feel free to update the answer whenever you want :) $\endgroup$
    – Tak
    Mar 14, 2017 at 10:30

First remove the meshes/objects with the following code:

for obj in bpy.context.scene.objects:
     if obj.type == 'MESH':
         obj.select = True
         obj.select = False

where you can check type against any of the following values according to [this documentation][1]:


Then you can execute the following code snippet to remove all the unused blocks. This way you don't have to close/open Blender or save/open a .blend file:

for block in bpy.data.meshes:
    if block.users == 0:

for block in bpy.data.materials:
    if block.users == 0:

for block in bpy.data.textures:
    if block.users == 0:

for block in bpy.data.images:
    if block.users == 0:

IMPORTANT NOTE: It looks like that there is some dependencies between some data blocks such as mesh, texture, image and materials. If you do not remove the data blocks on the highest level of the hierarchy, you will not be able to remove other data blocks or you have to take the risk and remove data blocks with users more than 0. So make sure you use the code above in the following order to remove data blocks. This way you can remove all unlinked (users == 0) data blocks:

remove meshes --> remove materials --> remove textures --> remove images

The followings also have data blocks:

  • $\begingroup$ This is more relevant to how you can prevent memory leakage in Blender $\endgroup$
    – Amir
    Mar 1, 2018 at 4:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .