Bulk remove entries from Airtable database using Google Apps Script?

I’m trying to clear out all the data in an Airtable table before adding new info. Right now I have a script that pulls album names and photo counts from Google Photos and puts them in Airtable. But I want to wipe the table clean first.

Here’s a simplified version of what I’m working with:

function updatePhotoData() {
  const API_KEY = 'your_key_here';
  const BASE_ID = 'your_base_id';
  const TABLE = 'PhotoAlbums';

  let photoAlbums = fetchGooglePhotoAlbums();
  let formattedData = photoAlbums.map(album => ({
    fields: {
      'Album Title': album.name,
      'Photo Count': album.imageCount
    }
  }));

  sendToAirtable(formattedData, API_KEY, BASE_ID, TABLE);
}

function fetchGooglePhotoAlbums() {
  // Code to get albums from Google Photos API
}

function sendToAirtable(data, key, base, table) {
  // Code to send data to Airtable
}

I’m pretty new to coding. Can someone help me add a step to delete everything in the Airtable before adding the new stuff? Thanks!

hey, i’ve dealt with this before. here’s a quick way to clear ur airtable:

function clearTable(apiKey, baseId, table) {
let records = getAirtableRecords(apiKey, baseId, table);
while (records.length) {
let batch = records.splice(0, 10).map(r => r.id);
deleteAirtableRecords(apiKey, baseId, table, batch);
}
}

just call this before adding new stuff. it’ll wipe everything fast. good luck!

I’ve worked with a similar setup before, and I can offer some advice on clearing your Airtable before adding new data. Instead of deleting records one by one, which can be slow for large datasets, you might want to consider using Airtable’s ‘deleteRecords’ endpoint. This allows you to delete up to 10 records in a single API call, which is much more efficient.

Here’s a function you could add to your script:

function bulkDeleteAirtableRecords(apiKey, baseId, table) {
  let allRecords = getAirtableRecords(apiKey, baseId, table);
  while (allRecords.length > 0) {
    let recordIds = allRecords.slice(0, 10).map(r => r.id);
    let url = `https://api.airtable.com/v0/${baseId}/${table}`;
    let options = {
      'method': 'DELETE',
      'headers': {
        'Authorization': `Bearer ${apiKey}`,
        'Content-Type': 'application/json'
      },
      'payload': JSON.stringify({records: recordIds})
    };
    UrlFetchApp.fetch(url, options);
    allRecords = allRecords.slice(10);
  }
}

You can then call this function at the start of your updatePhotoData function. This should significantly speed up the process of clearing your table before adding new data.

As someone who’s worked extensively with Airtable and Google Apps Script, I can offer a solution that might help. Instead of deleting records individually, which can be time-consuming, you can use Airtable’s batch delete feature. This allows you to remove up to 10 records at once, significantly speeding up the process.

Here’s a function you can add to your script:

function clearAirtableTable(apiKey, baseId, table) {
  let allRecords = [];
  let offset = '';
  do {
    let url = `https://api.airtable.com/v0/${baseId}/${table}?offset=${offset}`;
    let response = UrlFetchApp.fetch(url, {
      headers: { 'Authorization': 'Bearer ' + apiKey }
    });
    let data = JSON.parse(response.getContentText());
    allRecords = allRecords.concat(data.records);
    offset = data.offset || '';
  } while (offset);

  while (allRecords.length > 0) {
    let batch = allRecords.splice(0, 10).map(r => r.id);
    let deleteUrl = `https://api.airtable.com/v0/${baseId}/${table}`;
    UrlFetchApp.fetch(deleteUrl, {
      method: 'DELETE',
      headers: {
        'Authorization': 'Bearer ' + apiKey,
        'Content-Type': 'application/json'
      },
      payload: JSON.stringify({records: batch})
    });
  }
}

Call this function at the start of your updatePhotoData function to clear the table before adding new data. This approach should significantly improve the efficiency of your script.