Thomas Guggler
Thomas Guggler

Reputation: 9

google apps-script: how to delete all contacts fast ? timeout issue | People API

I am new in google apps-script try to delete contacts fast. Around 1500 contacts but only delete around 250 contacts in 6 Minutes then the timeout strikes.

try using people API:

function delAllContacts() {
  try {
    const people = People.People.Connections.list('people/me', {
      personFields: 'names,emailAddresses'
    });

    deleteContacts(people);

  } catch (err) {
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

How to do it right ?

The goal is to delete all contacts an reimport from a CSV-File every day

the script did not delete the contacts in under 6 minutes

how to get at list of: resourceName Because found this:

/**
 * Gets a list of people in the user's contacts.
 * @see https://developers.google.com/people/api/rest/v1/people.connections/list
 */
function delAllContacnts() {
  try {
    const people = People.People.Connections.list('people/me', {
      personFields: 'names,emailAddresses'
    });

  
    // Print the connections/contacts
    console.log('Connections: %s', JSON.stringify(people, null, 2));
  } catch (err) {
    // TODO (developers) - Handle exception here
    console.log('Failed to get the connection with an error %s', err.message);
  }
}

But need only the "people/c3735430325649075188" for example from each contact then my I can do this:

// Please set this object for deleting data.
const obj = { resourceNames: ["people/###", , ,] };

to delete a single contact this works for me but need to delete all because in 500 contact blocks

function remove_contact() {
// Please set this object for deleting data.
const obj = { resourceNames: ["people/c3150814607235737911"] };

const res = People.People.batchDeleteContacts(obj);
console.log(res);
}

remove_contact;

found here:

https://tanaikech.github.io/2022/11/08/using-getbatchget-batchcreatecontacts-batchdeletecontacts-batchupdatecontacts-of-people-api-with-google-apps-script/

Upvotes: 1

Views: 160

Answers (1)

Tanaike
Tanaike

Reputation: 201713

I believe your goal is as follows.

  • You want to delete "Around 1500 contacts" using "batchDeleteContacts" with Google Apps Script.

In the current stage, the method "batchDeleteContacts" can delete every 500 contacts. So, in your situation, it is required to use it in a loop. When this is reflected in a sample script, it becomes as follows.

Sample script:

Before you use this script, please enable People API at Advanced Google services. And, please set your all resource names of "Around 1500 contacts" to allResourceNames.

function myFunction() {
  const allResourceNames = ["people/###", , ,]; // Please set your all resource names of "Around 1500 contacts".

  // Split "allResourceNames" every 500 contacts.
  const reqs = [...Array(Math.ceil(allResourceNames.length / 500))].map(_ => allResourceNames.splice(0, 500)); // ref: https://github.com/tanaikech/UtlApp?tab=readme-ov-file#splitarray
  
  // Delete contacts.
  reqs.forEach(resourceNames => People.People.batchDeleteContacts({ resourceNames }));
}
  • When this script is run, all contacts of allResourceNames are deleted by splitting every 500 contacts.

Note:

  • This script supposes that you have permission to delete the contacts in allResourceNames. Please be careful about this.

Reference:

Upvotes: 0

Related Questions