Remove a workspace without it being active

You can remove a workspace with bpy.ops.workspace.delete() when it’s active. There does not seem to be a way to remove a workspace when it isn’t.
Other data in bpy.data has a remove function (for example bpy.data.images.remove()), while this is not there for the workspaces.

Is the function still missing or am I missing something?

Same problem here.
I’m trying to delete a workspace as the unregister process of an add-on

I tried with:

context.window.workspace = D.workspaces["my_workspace"]
#also with: context.workspace
bpy.ops.workspace.delete()

and

 override = context.copy()
 override['workspace'] = D.workspaces["my_workspace"]
 bpy.ops.workspace.delete(override)

in both cases the current workspace is deleted

I’m having a similar issue. I want to remove multiple workspaces, but context.workspace won’t update after running workspace.delete() the first time (in a single script), and so only the originaly active workspace is deleted, even after additional calls of 'workspace.delete()` in the same script.

Running bpy.data.workspaces.update() has no effect and overrides don’t seem to work either, as posted above.

Looked a bit into this. Apparently the operators don’t consider the context save from the window itself. Instead they first check if the mouse hovers a workspace tab, then grabs the ID from it, otherwise it will always fall back to window.workspace which is the active workspace.

The obvious fix would be to ensure the operators read the context so that it’s possible to pass an override.
So I submitted a patch.

3 Likes

Thank you so much, that is aweome!

@MACHIN3

In the meantime, I did find a way to delete any arbitrary id block, including workspaces.

bpy.data.workspaces (and a few others like screens etc.) don’t have a remove() method.

Turns out, it’s possible to feed any id block to bpy.data.batch_remove() as a tuple. It’s also possible to totally wreck the blend file, eg. removing all screens, workspaces, scenes etc :stuck_out_tongue:

import bpy

for ws in bpy.data.workspaces:
    if ws != bpy.context.workspace:
        bpy.data.batch_remove(ids=(ws,))
2 Likes

Legandary, I wasn’t even aware of batch_remove()!

workspaces = [ws for ws in bpy.data.workspaces if ws != context.workspace]
bpy.data.batch_remove(ids=workspaces)

Thanks again!