Purging unused data from Python

I found a lot of solutions for deleting unused data. Most of them are not practical for my case.
I have a very long script and during this, I want to purge all the unused data from the memory.

I'm trying to run this script but I keep getting errors:

import bpy
bpy.types.SpaceOutliner.display_mode = "ORPHAN_DATA"
bpy.ops.outliner.orphans_purge()


The error:

RuntimeError: Operator bpy.ops.outliner.orphans_purge.poll() failed, context is incorrect


Any suggestions?
Note: I can't reload blender; it's a long and continuous operation and can't be interrupted and if I ignored purging data the memory will leak.

• The problem indeed is about accessing the right UI in blender. How we can access a certain menu in the UI? – Mahmoud S. M. Shaqfa Dec 19 '18 at 11:25
• I believe what you are after is to override the context. blender.stackexchange.com/questions/15118/… – morph3us Dec 19 '18 at 12:43
• I tried this: bpy.context.area.type = 'OUTLINER' bpy.context.space_data.display_mode = 'ORPHAN_DATA' bpy.ops.outliner.orphans_purge() bpy.context.area.type = 'TEXT_EDITOR' But still I have error right now that the type doesn't exist! so can I specify a certain area of UI other than the current one (context)? – Mahmoud S. M. Shaqfa Dec 19 '18 at 13:19

override = bpy.context.copy()
override["area.type"] = ['OUTLINER']
override["display_mode"] = ['ORPHAN_DATA']
bpy.ops.outliner.orphans_purge(override)


you can call every operator with a dictionary in its call in first place (if there are more than one attributes to pass). this dictionary holds the information you want to override from the normal context. you dont have to name the override like this, any other name will do, too.

the thing is, i cannot help you a hundred percent, because I dont really know which parts of the context you have to overwrite. at least in 2.8, "area.type" and "display_mode" are not part of the original context, so you are trying to override something which isnt there, so the context still stays the same like without overriding at all.

i also dont know what you do have to overwrite, because right now i really dont have time to figure that out, sry, I am myself only learning all of this. you can however see whats part of the original context if you type following into the python console:

override = bpy.context.copy()
override


it will list you everything that is in the original context, maybe you find out what you want to overwrite.

• I'm not really expert in Blender's python.. but this should work.. The same error still appears but I'll try to fix it up.. if u can refine it I will be really thankful. – Mahmoud S. M. Shaqfa Dec 19 '18 at 14:28
• I just edited my comment, hope it helps! – morph3us Dec 19 '18 at 17:04
• It's not working at all I've tried to change it and fit it but seems useless in my case.. :( – Mahmoud S. M. Shaqfa Jan 3 '19 at 11:59
def purge() :
# print(C.area.spaces[0])
type_orig, C.area.type = C.area.type, 'OUTLINER'
# print(C.area.type)
# print(C.area.spaces[0])
C.area.spaces[0].display_mode = 'ORPHAN_DATA'
Op.outliner.orphans_purge()
C.area.type = type_orig


This is the closest I could come up with, considering that I could not find a way to create an Area instance using blender python. So I modify the current context and restore it to the previous value.