Auto Delete All The Records From All the Tables In Airtable

In this lesson, you will learn how to delete all the records from all the tables in the airtable script. Also, I am including how you can delete only a few tables by creating a list of them. Let's get started with deleting all the records. First, you need to install a scripting app in your base. Then copy and paste the below code into your scripting app.

: Code :


let all_tables = JSON.parse(JSON.stringify(base))['tables'];
let all_table_names = all_tables.map(record => record['name']);
for (let table_name of all_table_names) {await DeleteRecords(table_name);
}
async function DeleteRecords(table_name) {let table = base.getTable(table_name);
let records_raw = await table.selectRecordsAsync({fields: []});
let records = records_raw.records;
let records_ids = records.map(record => record.id);
while (records_ids.length > 0) {await table.deleteRecordsAsync(records_ids.slice(0, 50));
records_ids = records_ids.slice(50);
};}

Understanding the code:

Now when you run the above code into your app, It deletes all the records in the batches of 50 from all the tables. So, in the first two lines of code, we are getting all table's names. Then we are going through a loop and call a function. In this function, we are passing the table name as an argument. We are selecting the table and getting records. From those records, we are only getting ids on line no 13. And then again going through a loop and deleting all the records with Ids. That is the simple script that you can use in case you want to delete all the records from all the tables. If you don't know how to delete records with ids in airtable, then you can refer to existing articles or click here and learn that. Now, Let's learn how to delete all records from tables that are in our list. For that, copy and paste the below line of code and replace it with the first two lines of code.

: Code :


let all_table_names = ['Session_Logs','Students'];

So, when you execute this new code, it will only delete all records from the tables that you mention in the array. In this case, it deletes all the records of the 'Students' and 'Session_Logs' table. So now you know how to delete all the records from all the tables in the Airtable script. That's it for this lesson, stay connected for more learning lessons on airtable. Feel free to contact me for Airtable consulting services by CLICKING HERE.

COPYRIGHT © 2022 · Monino Solutions ·