Script to delete all groups in contacts

I would like to have a script to delete all my groups in my Contacts. Somehow I got a kind of replication bug resulting in over 500 groups.
Delete them one by one is not an option.

Tried this script, but it does not do the job, I have to enter to many group names and after a while the sript stops even when not all groups have been deleted.

tell application "Contacts" 
repeat 50 times 
set theGroup to group "MyGroupName" 
delete theGroup 
end repeat 
save
end tell

Any help will be much appreciated.


The reason that script doesn't delete all the bogus groups is that it only repeats 50 times, so it will only delete 50 in one go.

This script will delete every group with the name that you input (it asks you with a dialog box) until it can't find any more:

set counter to 0
display dialog "Name of group to delete?" default answer ""
set theGroupName to text returned of result
tell application "Contacts"
    repeat
        try
            set theGroup to group theGroupName
            delete theGroup
            set counter to counter + 1
        on error
            exit repeat
        end try
    end repeat
    save
    display dialog "Removed " & counter & " instances of the group" with icon caution buttons {"OK"} default button "OK"
end tell