# Proper way to remove unused linked Group data-blocks

In the outliner i have a local library called "Current File" and a linked library called "//foo.blend". Is this the proper way to remove all unused group-datablock-links from the "//foo.blend"-library?

import bpy

groups = bpy.data.groups
for group in groups:
if group.library and group.library.filepath == "//foo.blend":
if group.users == 1 and len(group.users_dupli_group) == 0: # EDIT
group.user_clear()
groups.remove(group)  # <- crash warning, don't use this part of the example.

• Using user_clear() can be dangerous, you should always try to get user count to 0 by clearing references that exist, see user_clear() – CodeManX Oct 4 '13 at 17:32
• @CoDEmanX, user_clear() is safe, but user_clear() followed by remove() can crash if the group was used anywhere. Best use user_clear() then save and reload. – ideasman42 Oct 5 '13 at 3:41
• but with len(group.users_dupli_group) == 0 added, it should be save? – ni-ko-o-kin Oct 5 '13 at 9:56
• @ni-ko-o-kin, no, because there are group users that are not dupli-groups (particle-systems for example). – ideasman42 Oct 6 '13 at 5:48