SQL query to delete empty groups of all couses

SQL query to delete empty groups of all couses

by Matti Jordman -
Number of replies: 4

Hi

Has anyone a ready SQL query to delete all empty groups of all courses (no members in the group)?

Thanks

MJ


Average of ratings: -
In reply to Matti Jordman

Re: SQL query to delete empty groups of all couses

by Matti Jordman -
Or maybe a php script to do this?
In reply to Matti Jordman

Re: SQL query to delete empty groups of all couses

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

All the usual disclaimers about messing around directly in the database being a good way to break your Moodle site apply.

The key tables are groups, groups_members and groupings_groups.

So probably something like

DELETE FROM mdl_groupings_groups
WHERE NOT EXISTS (
SELECT *
FROM mdl_groups_members
WHERE mdl_groupings_groups.groupid = mdl_groups_members.groupid
)

Then

DELETE FROM mdl_groups
WHERE NOT EXISTS (
SELECT *
FROM mdl_groups_members
WHERE mdl_groups.id = mdl_groups_members.groupid
)
Not tested. Probably safe. Probably, once you have done this, you then need to use Admin -> Development -> Purge caches.
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: SQL query to delete empty groups of all couses

by Andreas Grabs -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Translators
Hi,

I really wouldn't do that. Often there are some references to groups even if they are empty. For example hat could be an availability setting or some plugin that uses groups in its settings.
I'd rather use the core api to delete groups.

Best regards
Andreas
Average of ratings: Useful (1)
In reply to Tim Hunt

Re: SQL query to delete empty groups of all couses

by Matti Jordman -
Thanks! I'll be careful. My Moodle is very simple and it's probably safe to do this.
MJ