Help

Re: Delete all records on schedule

5519 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Slim_BENAZIZA
5 - Automation Enthusiast
5 - Automation Enthusiast

Hello !

So i have a base and i want to delete all records that are on the table every sunday (weekly).

i heard that it’s possible to do it with a script, but i don’t know how.

Can you help me please? Thanks

7 Replies 7

I don’t know Javascript, so somebody else will need to chime in to help you write a script that does this.

Alternatively, if you want a no-code way of deleting records on a schedule that doesn’t require any knowledge of coding or scripting, you could just use Make to do this.

It would be an extremely simple 2-step scenario that looks like the screenshot below:

image

Hi @Slim_BENAZIZA, you would create a new automation that runs every Sunday and the action would be the following script:

// LOAD TABLE
const table = base.getTable("[INSERT YOUR TABLE NAME]");
// LOAD ALL RECORDS IN THAT TABLE AND MAP ARRAY TO ONLY INCLUDE REC IDs
const records = await table.selectRecordsAsync().then(result => result.records.map(rec => rec.id);
// DELETE ALL RECORDS IN BATCHES OF 50 (AIRTABLE LIMIT) BY PASSING REC IDs
if(records) {
   while(records.length > 0) {
      await table.deleteRecordsAsync(records.slice(0,50));
      records = records.slice(50);
   }
}

I haven’t tested this in a base but it hopefully does the work :slightly_smiling_face: Otherwise please post here.

Hi @Rupert_Hoffschmidt

i did what you suggest, but when i test this script, i have an error:

Syntax error:
missing ) after argument list [script.js:7:97]

Sorry, missed a “)”:

// LOAD TABLE
const table = base.getTable("Table 3");
// LOAD ALL RECORDS IN THAT TABLE AND MAP ARRAY TO ONLY INCLUDE REC IDs
const records = await table.selectRecordsAsync().then(result => result.records.map(rec => rec.id));
// DELETE ALL RECORDS IN BATCHES OF 50 (AIRTABLE LIMIT) BY PASSING REC IDs
if(records) {
   while(records.length > 0) {
      await table.deleteRecordsAsync(records.slice(0,50));
      records = records.slice(50);
   }
}

Screenshot 2022-11-02 15.40.46

Ah damn sorry — didn’t have more than 50 records when I tested!

// LOAD TABLE
const table = base.getTable("Table 3");
// LOAD ALL RECORDS IN THAT TABLE AND MAP ARRAY TO ONLY INCLUDE REC IDs
let records = await table.selectRecordsAsync().then(result => result.records.map(rec => rec.id));
// DELETE ALL RECORDS IN BATCHES OF 50 (AIRTABLE LIMIT) BY PASSING REC IDs
if(records) {
   while(records.length > 0) {
      await table.deleteRecordsAsync(records.slice(0,50));
      records = records.slice(50);
   }
}

Now it should work :slightly_smiling_face:

That doesn't work if the table is empty (the map).

//
// Remove all existing stats ahead of recompute
//
let records = await statsTable.selectRecordsAsync({ fields: [ "RecordID"]});
if (records) {
    let allRecordIDs = records.recordIds;
    let totalCount = allRecordIDs.length;

    while (allRecordIDs.length > 0) {
        const maxRecords = 50;
        let count = (allRecordIDs.length > maxRecords) ? maxRecords : allRecordIDs.length; 
        await statsTable.deleteRecordsAsync(allRecordIDs.slice(0,count));
        allRecordIDs = allRecordIDs.slice(count);
    }
}