Delete all contacts that belongs to a specific one ContactGroup

177 views
Skip to first unread message

Andrea Bonamici

unread,
Apr 2, 2019, 11:13:24 AM4/2/19
to Google Apps Script Community

Hi all.
I need an help, please.

I want to delete all contacts that belong to a specific ContactGroup.
This code working but it's too slow (contacts are more than 2500)

function TestDeleteContacts() {
var group  = ContactsApp.getContactGroup('NameOfGroup');
var contacts = ContactsApp.getContactsByGroup(group);
for (var i=0; i<contacts.length; i++) {
contacts[i].deleteContact();
}
};


Is there a way or a direct command without using the for loop?


Thank you all for your help!
Best regards
Andrea

Bauke de Kroon

unread,
Apr 2, 2019, 11:30:30 AM4/2/19
to Google Apps Script Community
Hi Andrea,

Looking at the ContactsApp API you could use "deleteContactGroup(group)" to delete a specific contact group.


// The code below creates deletes the contact group named "Work Friends"
var group = ContactsApp.getContactGroup("Work Friends");
ContactsApp.deleteContactGroup(group);



Regards,

Bauke

Reno Blair

unread,
Apr 2, 2019, 11:44:46 AM4/2/19
to google-apps-sc...@googlegroups.com
Hi Andrea,

Unfortunately there isn't any batching available to delete contacts, so you have to do them one at a time. This is also the same with the People API.

Removing the for loop won't really make it any quicker, but just for kicks:
ContactsApp.getContactGroup('NameOfGroup').getContacts().forEach(function(contact) {contact.deleteContact();});


Disclaimer: This email and any files transmitted with it, are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify G-workplace and destroy this message. G-workplace B.V. has it registered offices in The Hague, The Netherlands and is registered with the Chamber of Commerce under no. 52204316.



--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/8ec845db-d0e6-4004-815b-721930749518%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Reno Blair
Educational Technology Services​

Andrea Bonamici

unread,
Apr 2, 2019, 11:44:51 AM4/2/19
to Google Apps Script Community
Thank you very much Bauke !!!!
It's worked.

in my research I always found examples that used the for loop to delete contact and I never tried this simple command. 
I am at my first experiences ... be patient :D

You have been very kind and very precious. 
Get ready because I have other questions :D

GRAZIE
Andrea

Reno Blair

unread,
Apr 2, 2019, 11:47:22 AM4/2/19
to google-apps-sc...@googlegroups.com
Just to be clear - you've only removed the contact group, not the contacts as your original request and code indicated was your intention.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-apps-script-community.

For more options, visit https://groups.google.com/d/optout.

Andrea Bonamici

unread,
Apr 2, 2019, 12:05:26 PM4/2/19
to Google Apps Script Community
You are right. Sigh !
The contacts remain. 
Only the label has disappeared :(

I opened the champagne for nothing :)

Do you confirm that there is no way to speed up this contact cancellation? 
Very frustrating

Thank you all for your help.
Andrea

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

ChiefChippy2 is awesome

unread,
Apr 2, 2019, 12:16:39 PM4/2/19
to Google Apps Script Community
Why don't you use the delete button on the contacts UI?


On Tuesday, April 2, 2019 at 5:13:24 PM UTC+2, Andrea Bonamici wrote:
AAD0797D-7E9E-40F3-83D3-49FDE0C38B87.jpeg

Reno Blair

unread,
Apr 2, 2019, 12:53:16 PM4/2/19
to google-apps-sc...@googlegroups.com
Unfortunately, that still only deletes the label. However, the the UI does indeed facilitate the requested functionality:
  1. Go to the label.
  2. Select one of the contacts (checkbox, not to view/edit).
  3. Select all with the checkbox menu at the top.
  4. Go to the three dot menu and click delete to delete all selected contacts.
image.png

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Dimu Designs

unread,
Apr 2, 2019, 4:44:26 PM4/2/19
to google-apps-sc...@googlegroups.com
Given the volume of contacts you want to delete, your best option when it comes to speed is to leverage the REST People API directly and target its batch endpoint with a multi-part request.

Even that may not be fast enough given that there is a max of 100 operations per batch. Still, you'll end up reducing the number of HTTP requests to 25 where before you had 2500 (every time you call deleteContact(), behind the scenes you're making an HTTP Request to delete a contact).

If you want to give it a go look up the discovery document for the People API to find its batch endpoint and then look up how to make a multi-part request.

The reference documentation for the People API does not provide any guidance specific to making multi-part batch requests. But other APIs do (see list below) and you can use them as reference when building your own implementation.

GMail API Batch Request Guide

Andrea Bonamici

unread,
Apr 3, 2019, 6:05:36 AM4/3/19
to Google Apps Script Community
Good morning. Thank you all for the answers!!!
I did another test.

If I create contacts with the CODE 1)
is sufficient CODE 2) to delete ContactGroup and
all contats that belong it.
***Isn't necessary delete contact one to one.***


I probably have a bit of confusion in my head between 
ContactGroup and Label

CODE 1) 
function createContact() {
  var GroupName = "SharedContact";  //Name of contact label
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 300;   // Number of rows to process

  //DELETE PREVIOUSLY CREATED GROUP
  var group = ContactsApp.getContactGroup(GroupName);
  
  if (group != null) {
    var group = ContactsApp.getContactGroup(GroupName);
    ContactsApp.deleteContactGroup(group);
     } 
  
   
  
  //CREATE GROUP
  var group = ContactsApp.createContactGroup(GroupName);  
  
 
  // Fetch the range 
  var dataRange = sheet.getRange(startRow, 1, numRows, 8)

  
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
   // Logger.log (data);
    for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var firstName = row[0]
    
    // IF FIRSTNAME IN ROW IS NULL EXIT SCRIPT
    if (firstName == 0) return;
                           
    var lastName = row[1]
    var emailAddress = row[2]
    var phone = row[3]
    var company = row[4]
    var notes = row[5]
    var address = row[6]
    var DeleteContact = row[7];

    
// Create contact in Google Contacts
if (DeleteContact != "Yes") {
var contact = ContactsApp.createContact(firstName, lastName, emailAddress);

        // Add values to new contact
        contact.addCompany(company, "");
        contact.addPhone(ContactsApp.Field.WORK_PHONE, phone);
        contact.setNotes(notes);
        contact.addAddress(address, address);
        contact.addToGroup(group);
      };
     
    };
};
//#################################


CODE 2)
function deleteContacts2() { 
// The code below creates deletes the contact group 
var group = ContactsApp.getContactGroup("SharedContact");
ContactsApp.deleteContactGroup(group);
}

Best regards
Andrea
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages