Deleting cache_file datablocks from a file

Is there a good way to go about deleting cache data-blocks in BPY? I’ve been building a cache manager, but have ran into the issue of caches not having a remove or pop attribute like most other data types. I might have been using pop specifically wrong, but I assumed since it was an inherited function I could just throw it at the end of bpy.data.cache_files and it would work with the cache name.

Some data block collection properties don’t have a remove method. I remember bpy.data.screens in particular has the same issue.

To remove those, Bastien (I think) added bpy.data.batch_remove() (docs), which takes a list or a sequence like a collection property.

Maybe something like this:

bpy.data.batch_remove(bpy.data.cache_files)

Just as you responded I found users_clear().

It seems to do the job, but I get a build error in the console right before cleaning up orphans.

ERROR (bke.lib_id): D:\blender-git\blender\source\blender\blenkernel\intern\lib_id.c:311 id_us_min: ID user decrement error: CFtest_cache.abc 
(from '[Main]'): 0 <= 0

My code works fine and nothing seems unstable, nor does the error code appearing after deleting the cache.

Ill try your method, but any thoughts on this one?

Clearing users “orphans” the data block so it isn’t saved the next time the file is saved. You then need to purge orphans to have them removed from the file. Using batch_remove tags the data blocks passed for deletion, then removes them immediately.

On release builds I wouldn’t worry too much about the message, on debug builds it would have triggered an assert and crashed, but looking at the comments inside void id_us_min(ID *id), they anticipate Blender not being able to track user changes (user_clear simply sets the data block user count to 0, nothing is decremented).

Yup, seems like batch_remove is the cleaner option… Thanks a bunch!

1 Like