Delete all unused textures from Blender using Python

With large projects that have hundreds of textures, it can be very difficult to deal with them in the UV image editor using the following little drop-down menu.

The best solution that I've come with so far is to modify the UV Image Editor code using some code from the Sculpt and Texture Paint section which is very helpful but it's still difficult to tell if something is in use and it has to be reopened every time something is selected and deleted.

(It now looks like the following in case anyone is interested. You can do this by menu-clicking the image icon next to New and Open then selecting edit source and changing the following line in space_image.py

layout.template_ID(sima, "image", new="image.new", open="image.open")


to be

*layout.template_ID_preview(sima, "image", new="image.new", open="image.open", rows=8, cols=16*)


This question deals with deleting textures and it's what I already do. But it's time consuming and very difficult to find the unused textures when there are a couple hundred of them, especially if the drop down menu has to be reopened and searched for everyone one of them.

So my question is the this:
Is there a way to delete all the unused textures using a Python script?

I have found one script that does not work very well. Sometimes it does and sometimes it does not.
This gives the impression that there is something going on behind the scenes when it comes to how textures are considered to be in use.

By the way, I don't use Fake User or pack images so this may simplify things a bit.

• If the datablocks are unused, they should be cleared out when you reload the .blend.. – gandalf3 Jul 8 '14 at 9:08
• The problem is having to reopen and search the menu over and over again since it closes when something is selected. Also, it's not always clear what needs to go just from looking. – MarcClintDion Jul 8 '14 at 9:26
• I'm not sure I understand.. Couldn't you just save the .blend and re-open it? (Ctrl+S, then Ctrl+Shift+O 1) That should clear out all datablocks with no users. – gandalf3 Jul 8 '14 at 17:46
• I do that but it does not work all the time. I've even tried deleting every last object from the scene, saving, then reloading it, but most of the textures were still there even though there were not any models. This works fine for removing materials but textures are really hard to get rid of. – MarcClintDion Jul 9 '14 at 2:18
• Happily that's one of the things that's being refactored for 2.8 (some of it might even be in 2.78) – gandalf3 Aug 4 '16 at 2:09

Remove all images with zero users from the blendfile:

import bpy

for img in bpy.data.images:
if not img.users:
bpy.data.images.remove(img)

• Do you know if anyone written an addon to have this option accessible right from the UV Image Editor? – Perrishnikov Aug 20 '15 at 15:55
• How can I remove all texture without users OR with fake users? – RollRoll Oct 25 '15 at 3:31
• ID.use_fake_user – pink vertex Oct 27 '15 at 23:14
• if not img.users or (img.users == 1 and img.use_fake_user): – pink vertex Oct 27 '15 at 23:16

fix as 0 use ...press ctrl and delete " texture , material and others" and after, save your work

• "Using Python". – Shady Puck Aug 2 '16 at 9:03
• Please expand your answer so it is clear what steps need to be taken. Also consider that the question asks for an answer using Python. – Ray Mairlot Aug 2 '16 at 13:19