WELCOME

This blog is where I post about my consulting work with Microsoft Technologies, and other random tidbits that don't fit in my Photo Blog or my Iraq Blog.

Tuesday, February 2, 2010

Souce code for deleting Google/Gmail Contact Groups

Over a year ago I posted a little utility for deleting unwanted groups out of your Gmail Contacts.

Blog post here: http://blog.scrappydog.com/2009/01/deleting-4000-gmail-groups-or-repairing.html

Application install here: http://scrappydog.com/GoogleGroupCleanup/publish.htm

I wrote this for my own use because a bad replication tool had created 10,000+ bogus groups in my Contacts, and I REALLY didn't want to delete them by hand!

Apparently, it has saved a fair number of other people's bacon as well...

Today "Ben" asked if I could post the source code for my tool? And so here it is (not much to it):


using System;
using Google.GData.Contacts;

namespace GoogleGroupCleanup
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("Google Gmail Group Cleanup Tool");
Console.WriteLine();
Console.WriteLine("Written by: Eric Bowen (googlegroupcleanup@scrappydog.com)");
Console.WriteLine();
Console.WriteLine("This application is provided WITHOUT any guarantee. Use at your own risk.");
Console.WriteLine();
Console.WriteLine("This application will delete ALL groups from your Gmail account.");
Console.WriteLine("It will NOT delete any of your contacts or email.");
Console.WriteLine();
Console.WriteLine(
"This application deletes up to 1,000 groups at a time. If you have more than 1,000 groups to delete you will need to run it multiple times.");


Console.WriteLine();
Console.Write("Enter Google email address (this will not be stored): ");
string email = Console.ReadLine();

Console.WriteLine();
Console.Write("Enter Google password (this will not be stored): ");
string password = Console.ReadLine();

var service = new ContactsService("gmail-group-cleanup");
service.setUserCredentials(email, password);

var groupQuery = new GroupsQuery(GroupsQuery.CreateGroupsUri("default"));
groupQuery.NumberToRetrieve = 1000;
var groupsFeed = service.Query(groupQuery);

int counter = 0;
foreach (GroupEntry group in groupsFeed.Entries)
{
Console.WriteLine(group.Content.Content);
group.Delete();
counter++;
}

Console.WriteLine();
Console.WriteLine(counter.ToString() + " groups deleted");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception:");
Console.WriteLine(ex.Message);
Console.WriteLine();
}
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
}
}