Putting this here because the site that shall not be named has a limited retention time (I believe). Basically just loops though all the bpy.data collections that have a remove() method and calls it if the object (mesh, image, whatever) has no users.
Simple, silly little thing – use with care because you will lose data.
import bpy
class RemoveDatablocksOperator(bpy.types.Operator):
bl_idname = 'world.remove_redundant_datablocks'
bl_label = 'Remove Redundant Datablocks'
@classmethod
def poll(cls, context):
return True
def execute(self, context):
data_blocks = ['actions',
'armatures',
'brushes',
'cameras',
'curves',
'fonts',
'groups',
'images',
'lamps',
'lattices',
'materials',
'meshes',
'metaballs',
'objects',
'particles',
'scenes',
'texts',
'textures',
'worlds']
for item in data_blocks:
db = getattr(bpy.data, item)
for i in db:
if i.users <= 0:
db.remove(i)
return {'FINISHED'}
if __name__ == '__main__':
bpy.ops.world.remove_redundant_datablocks()