Google Apps Script: Clear all airtable table data before adding new records

Need help with clearing airtable records before inserting new ones

I’m working on a Google Apps Script that pulls playlist info from Google Photos and saves it to an airtable table. The problem is that every time I run it, the old data stays there and new data gets added on top.

I want to wipe out all existing rows in the table first, then add the fresh data. Here’s what I have so far:

function updatePlaylistInfo() {
  var token = ""; // your api token goes here
  var workspaceId = ""; // your workspace id here
  var sheetName = "sheet1"; // your sheet name here

  // Fetch playlists from Google Photos
  var playlists = [];
  var pageToken = null;
  do {
    var result = fetchPhotoPlaylists(pageToken);
    if (result.albums && Array.isArray(result.albums)) {
      playlists = playlists.concat(result.albums);
    }
    pageToken = result.nextPageToken;
  } while (pageToken);

  // Format data for airtable
  var dataRows = [];
  playlists.forEach(function(playlist) {
    var row = {
      "fields": {
        "Playlist Title": playlist.title,
        "Photo Count": playlist.mediaItemsCount
      }
    };
    dataRows.push(row);
  });

  // Send data to airtable
  pushDataToAirtable(dataRows, token, workspaceId, sheetName);
}

function fetchPhotoPlaylists(token) {
  var requestOptions = {
    method: "GET",
    headers: {
      "Authorization": "Bearer " + ScriptApp.getOAuthToken()
    },
    muteHttpExceptions: true
  };

  var endpoint = "https://photoslibrary.googleapis.com/v1/albums";
  if (token) {
    endpoint += "?pageToken=" + token;
  }

  var apiResponse = UrlFetchApp.fetch(endpoint, requestOptions);
  var responseData = JSON.parse(apiResponse.getContentText());

  return responseData;
}

function pushDataToAirtable(dataRows, token, workspaceId, sheetName) {
  var endpoint = "https://api.airtable.com/v0/" + workspaceId + "/" + sheetName;
  var requestHeaders = {
    "Authorization": "Bearer " + token,
    "Content-Type": "application/json"
  };

  // Split into chunks of 10
  var chunkSize = 10;
  var chunks = [];
  while (dataRows.length > 0) {
    chunks.push(dataRows.splice(0, chunkSize));
  }

  // Process each chunk
  chunks.forEach(function(chunk) {
    var requestData = {
      "records": chunk
    };

    var requestOptions = {
      "method": "POST",
      "headers": requestHeaders,
      "payload": JSON.stringify(requestData)
    };

    UrlFetchApp.fetch(endpoint, requestOptions);
  });
}

I’m pretty new to coding and used AI to help write this. How can I add a function to clear all existing records before the new ones get written? Any help would be great!

hey! to clear your airtable, you gotta pull all record ids first, then delete 'em in batches. add a deleteAllRecords function before pushDataToAirtable that gets existing records, collects their ids, and sends DELETE requests in chunks of 10. check the airtable api docs for help!