Unlink all children from the scene collection
Not a fan of using outliner operators in scripts. Unlinking or removing the collections from a scene will also have the same effect of clearing a scene.
Script to unlink all the child collections from the scene's master collection.
import bpy
context = bpy.context
scene = context.scene
for c in scene.collection.children:
scene.collection.children.unlink(c)
Theoretically could remove the child collections using bpy.data.collections.remove(c)
above instead of unlink, however I found this has the tendency to seg fault my build of 2.8._ (See EDIT below)
Running this after removes the orphan collection
import bpy
for c in bpy.data.collections:
if not c.users:
bpy.data.collections.remove(c)
Similarly could remove the collections objects from blender file with
for o in c.objects:
bpy.data.objects.remove(o)
EDIT. Update: removing collections from bpy data
import bpy
name = "Collection 1"
remove_collection_objects = True
coll = bpy.data.collections.get(name)
if coll:
if remove_collection_objects:
obs = [o for o in coll.objects if o.users == 1]
while obs:
bpy.data.objects.remove(obs.pop())
bpy.data.collections.remove(coll)
bpy.ops.outliner.collection_delete(hierarchy=True)
is the part, where the error occurs. But the provided solution to unlink the contents works perfectly. $\endgroup$