Nov 02, 2022 04:06 AM
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
Nov 02, 2022 04:15 AM
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:
Nov 02, 2022 04:27 AM
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.
Nov 02, 2022 06:18 AM
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]
Nov 02, 2022 07:00 AM
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);
}
}
Nov 02, 2022 07:41 AM
Nov 02, 2022 09:28 AM
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:
Feb 01, 2024 02:30 PM
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);
}
}