We can delete collections in Blender through the “collections” list in “bpy.data”.
To remove a collection we need to use the “remove” method from the collections list, specifying the collection to be removed in the parameter:
1 2 3 |
collection = bpy.data.collections.get('collection_to_del') bpy.data.collections.remove(collection) |
When we delete a collection in this way, all objects from the collection will be unlinked from the scene, but will not be deleted as objects and remain in the blend-file.
To completely remove a collection and all objects in it, we must first delete the objects from the “objects” list, and then delete the collection itself.
This code removes the collection and all nested objects:
1 2 3 4 5 6 7 8 |
import bpy collection = bpy.data.collections.get('collection_to_del') for obj in collection.objects: bpy.data.objects.remove(obj, do_unlink=True) bpy.data.collections.remove(collection) |