Purge Orphan Data

The third time I ask for a practical solution to purge data of a running code and (keep the code running after) and no one good solution worked for my case. I guess I don't get it at one part or missing something!

Previous questions: Garbage collection in Blender

Purging unused data from Python

The following code keeps raising errors and it's working fine in a separate file but not in the code's file.

if purge_idx == purge_max:
#bpy.ops.wm.save_mainfile(filepath=bpy.data.filepath)
#bpy.ops.wm.open_mainfile(filepath=bpy.data.filepath)
current_screen = bpy.context.area
current_screen.type = 'OUTLINER'
current_subwin = bpy.context.space_data
current_subwin.display_mode = 'ORPHAN_DATA'
bpy.ops.outliner.orphans_purge()


current_screen.type = 'OUTLINER' AttributeError: 'NoneType' object has no attribute 'type'

I tried even to load and reload the documents but I lose the current context in other well-functioning methods again (Context error). What is exactly going on with this code? I spent too much time working on it with different overriding methods but still useless.

I appreciate your help my code is recursive and complex and keeps packing orphan data and the script crashes after Blender's memory reaches a certain limit (Expected) how to solve this issue?

Simply I want to empty the unused (no-user/not referenced) data recursively during the process without interrupting the process. Any solution would help I'm ready to use.

Note: I checked all possible solutions on the web but seems useless.