I want to quickly minimize the size of a Blender file.
How do I delete any data that is not being used?
If you're talking about materials or textures:
In the Node Editor, all the materials that have a "0 something" name have no user, it means that they are not used by any object and that they will automatically be deleted when you'll close the file.
You can delete them for good immediately if you go to the Outliner panel, select Orphan Data in the dropdown menu, then select all the materials displayed in the materials category and right click > Delete.
If you want to delete materials that have users, select them one by one in the Node editor and click both on shift and on the "X" button on the right of the material name. Now they will have a "0" displayed in front of their name, so again, they will be deleted when you'll close the file, or you can delete them right away in the Outliner.
After deleting materials, if some images (those that have been used by some materials) are also displayed in the Outliner > Orphan Data > image category, again, select them all and right click > Delete.
If you already know by their name the materials and images you want to delete you can directly go to the Outliner then select Blender File in the dropdown menu and Delete all the unwanted materials and images.
My solution is a based on using Blender's Python API. There is a couple of ways. First, you can reset everything to factory settings by the following:
Or you can simply store the current .blend file and re-open it as follow. First save the .blend file:
bpy.ops.wm.save_as_mainfile(filepath=savePath) # savePath contains something like `/blendFilesOnDisk/myBlend.blend
And then re-open it using:
However, if you want to keep your current settings or you do not want to reload the .blend file you can instead remove the unneeded data blocks manually. For instance, after you remove the meshes/objects in your scene you can execute the following code to remove all the unused data blocks that were assigned to those meshes/objects:
for block in bpy.data.meshes: if block.users == 0: bpy.data.meshes.remove(block) for block in bpy.data.materials: if block.users == 0: bpy.data.materials.remove(block) for block in bpy.data.textures: if block.users == 0: bpy.data.textures.remove(block) for block in bpy.data.images: if block.users == 0: bpy.data.images.remove(block)
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. So make sure you follow the order to remove the data blocks:
meshes --> materials --> textures --> images
The followings also have data blocks that you can remove but they typically do not take that much space:
bpy.data.curves # for curves bpy.data.lamps # for lamps bpy.data.cameras # for cameras