Friday, January 10, 2014

Deleting multiple empty Google contact groups

My beloved Google Contacts suddenly have ~300 empty "Starred in Android" groups. Not sure how or why they showed up, but many people who have the very same problem are pointing fingers at Nook, and yes, I was using a Nook at the time it occurred.

Anyway, there does not seem to be an easy way to delete all these empty contact groups except manually one by one, which I simply refuse to do. So I wrote a simple script for Google Apps Script to solve the problem. The script is listed below.

Disclaimer: I have tested the script many ways, but please use at your own risk !

To run simply:
- Go to https://script.google.com,
- Start a blank project, and copy/paste the script below
- Run once with ContactsApp.deleteContactGroup commented out (as is)
- Press Ctrl-Enter to view logs to make sure everything looks fine.
- Uncomment the ContactsApp.deleteContactGroup line, and run again to delete empty groups.
- If you want to remove all empty groups independent of name, uncomment the line referencing "Starred in Android"

Nifty,
Thomas

// ============================================================
// What:  Deletes all empty "Starred in Android' contact groups from your Google contacts
// How:   1) Run once to verify that correct groups will be deleted.
//           Review log file (press ctrl-enter)
//        2) Uncomment deleteContactGroup line below and rerun.
//
// Jan 2014, Thomas Gemal
// ============================================================
function DeleteAllEmptyGroups() {
  // Grab all contact groups
  var myContactGroups = ContactsApp.getContactGroups();

  // Use this to limit number of groups processed if performance is an issue
  var MAXCOUNT = 1000;
  var count = 0;
  
  // For each contact group
  for(var i = 0; i < myContactGroups.length; i++) { 

    // Only delete MAXCOUNT groups at a time (since execution can take long)
    if (count == MAXCOUNT) {
      break;
    }
    
    // Get name of a contact group
    var group = myContactGroups[i];
    var group_name = group.getName();

    // Keep this code to only deal with "Starred in Android". 
    // To delete all empty groups, simply comment this 'if' out
    if (group_name != "Starred in Android") {
      Logger.log(Utilities.formatString("%s: Skipped. Not SiA", group_name));
      continue;
    }

    // Get number of contacts for a group
    var num_contacts = ContactsApp.getContactsByGroup(group).length;

    // We are only looking for empty groups
    if (num_contacts == 0) {
      // Ignore special groups
      if (group_name.substring(0,13) != "System Group:") {
        // Log that we want to delete
        Logger.log(Utilities.formatString("%s (%d):  DELETE",group_name,num_contacts));
        count++;
      
        // !!!!! Uncomment line below to physically delete the groups  !!!!!
        // Make sure you have first verified that list to delete is OK
        // ContactsApp.deleteContactGroup(group);
      } else {
        Logger.log(Utilities.formatString("%s (%d):  Skipped. System group",group_name,num_contacts));
      }   
    } else {
      Logger.log(Utilities.formatString("%s (%d):  Skipped. Not empty",group_name,num_contacts));
    }   
  }
  Logger.log(Utilities.formatString("Empty groups processed: %d", count));
}

33 comments:

  1. My alternative (although yours looks better) uses iMacros. I have linked to your blog from post as an alternative to mine. I hope that's ok.

    Your feedback is appreciated.
    http://j.mp/1eTTvER

    ReplyDelete
  2. Thomas, I have many empty google groups - not "starred in Android", but many. How might I modify your script to delete these other empty groups? Thanks...

    C. E. Smith

    ReplyDelete
  3. @CE Smith: Simply find the line saying "To delete all empty groups, simply comment this 'if' out". Then delete the 4 lines below, from "if" to "}". Then run script.

    ReplyDelete
  4. The script keeps getting timed out and sometimes fails to run. I've just been rerunning it over and over again. It deletes only a few groups at a time. Am I doing something wrong? I can almost delete them one by one by hand at that speed but I have so many.

    ReplyDelete
  5. @RevChuck, your issue is likely due to too many empty accounts. Change MAXCOUNT to a lower number, eg 500.

    ReplyDelete
  6. Thanks!!!
    Like you I refused to delete these one at a time. I didn't know this scripting language existed now that I do I may start programming more such tools!!!

    ReplyDelete
  7. Wow, after days of searching...
    I finally get rid of thousands empty Google Groups!
    Unbelievable that Google doesn`t offer a solution for this problem!

    THANK YOU VERY VERY MUCH FOR YOUR SCRIPT!

    best regards, Jue Bilstein



    ReplyDelete
  8. I have over 40 System Group: My Contact groups that it won't let me delete. Any way around this?

    I commented out the system group logic, but looks like there is something that does not allow the deletion of system groups.

    ReplyDelete
  9. @Greg, not sure about system groups. Maybe special permissions (or something like that are needed). Please also verify that you have the exact group name specified correctly.
    /T

    ReplyDelete
  10. Name is correct. It is an error thrown by the script saying "System groups cannot be deleted. (line 49, file "Code")".

    Definitely a permission issue. Unfortunately I have about 60 of these empty groups! Deleting manually is not something I really want to do.

    ReplyDelete
  11. This is very useful! Thanks very much, worked like a dream

    ReplyDelete
  12. OMG!!!
    i would definitely give this a salute as it totally works for me!!

    Thank you Thomas!!

    your are my hero!!

    ReplyDelete
  13. That is absolutely great script. I wasn't aware of these possibilities in Google Docs.
    Thank you very much, you saved me few hours of dumb deleting empty groups!

    ReplyDelete
  14. Thanks!!! Worked like a charm, and extra thanks for the guidelines for novice script users like me. The groups list can now be useful for me.

    ReplyDelete
  15. Wow... even a total nitwit could use this! Thank you so much, worked after commenting the line about starred in Android and removing the extra }

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. This comment has been removed by the author.

    ReplyDelete
  18. Finally!!!! All duplicates are gone, thank you so much!
    Just as a note, it took quite some time on my computer and I could only delete 125 at a time ... so patience was required. But hey, I have waited for years to delete them!!!
    Best, Tom

    ReplyDelete
  19. I can only see the "starred in android" on my phone I actually also have "family" and "coworker". However I can't see any of them on the desktop.

    When I run your script I get no errors but also no list of groups in the log.

    What am I doing wrong

    ReplyDelete
  20. I ran this to delete multiples with content, I also deleted individual groups by adjusting your code. It worked perfectly.
    Thank you,
    Bob

    ReplyDelete
  21. Very useful tool.
    Thanks so much.

    Been looking for something like this for years!!

    ReplyDelete
  22. Excellent Script!! My wife was having issues with HUNDREDS of "starred in android" entries on her phone in the contacts settings and we were having a heck of a time figure out what the deal was. I did have to tweak the code slightly to have it look at not only empty groups so changed "(num_contacts == 0)" to "(num_contacts >= 0)". This picked up and deleted almost 900 "Starred in Android (1)" entries. My wife was on the verge of just deleting and re-entering all her contacts, so Thank You Thank You!!

    ReplyDelete
  23. Used this to clear out 2000+ Starred in Android entries that was causing my phone's Google account to freak out when it tried to sync contacts. Worked perfectly. BIG thanks for posting this and to the commenters who offered other tweaks for the script.

    ReplyDelete
  24. Brilliant! I just got rid of about 500 empty "SiA" groups!

    ReplyDelete
  25. Wow !
    Thanks !
    so much time seeking for a way to get rid of bunch of empty groups (duplicates dues to crappy Outlook Syncs...)
    Thanks again

    Angelo

    ReplyDelete
  26. It's unbelievable how easily I got rid of these empty groups. Thomas, thank you so much for the script! It works like magic.

    ReplyDelete
  27. I modified it and used the "run, my function" and nothing happens.. here is the entire code:

    function myFunction() {
    function DeleteAllEmptyGroups() {
    // Grab all contact groups
    var myContactGroups = ContactsApp.getContactGroups();

    // Use this to limit number of groups processed if performance is an issue
    var MAXCOUNT = 1000;
    var count = 0;

    // For each contact group
    for(var i = 0; i < myContactGroups.length; i++) {

    // Only delete MAXCOUNT groups at a time (since execution can take long)
    if (count == MAXCOUNT) {
    break;
    }

    // Get name of a contact group
    var group = myContactGroups[i];
    var group_name = group.getName();

    // Keep this code to only deal with "Starred in Android".
    // To delete all empty groups, simply comment this 'if' out


    // Get number of contacts for a group
    var num_contacts = ContactsApp.getContactsByGroup(group).length;

    // We are only looking for empty groups
    if (num_contacts == 0) {
    // Ignore special groups
    if (group_name.substring(0,13) != "System Group:") {
    // Log that we want to delete
    Logger.log(Utilities.formatString("%s (%d): DELETE",group_name,num_contacts));
    count++;

    // !!!!! Uncomment line below to physically delete the groups !!!!!
    // Make sure you have first verified that list to delete is OK
    // ContactsApp.deleteContactGroup(group);
    } else {
    Logger.log(Utilities.formatString("%s (%d): Skipped. System group",group_name,num_contacts));
    }
    } else {
    Logger.log(Utilities.formatString("%s (%d): Skipped. Not empty",group_name,num_contacts));
    }
    }
    Logger.log(Utilities.formatString("Empty groups processed: %d", count));
    }
    }

    ReplyDelete
  28. WIlliam, when you say that nothing happens, does that mean that you followed all the instructions on top, and when looking at the log file, it was empty?

    ReplyDelete
  29. This is fantastic. Thanks for writing and sharing it.

    ReplyDelete

Note: Only a member of this blog may post a comment.