How to Delete Records in Airtable with Scripting

In this article, I am explaining to you how to Delete Records in Airtable with Scripting. Till now you already learned CRUD operations in airtable scripting except delete operation in previous articles. Let's learn this last part of the CRUD operation of deleting records in this lesson.

First of course you need airtable scripting app to be installed in your base. Then Let's assume that we would like to delete all the records with certain conditions. Before I explain the condition, Let's have a look at the table we are using in this lesson.

airtable script delete record how to delete records in airtable with scripting airtable scripting delete records in airtable

Above is the table we are using for this lesson. In this table, we have records of teachers. If any teacher is a spare teacher then the checkbox in the table is checked. Our condition for this example is to delete all the records of spare teachers. Simply all the records with the 'is spare teacher?' checkbox checked should be deleted.

Copy and paste the below code into your scripting app and run the script. It deletes all the records of spare teachers from the table.

: Code :


let table = base.getTable('Teachers');let table_data = await table.selectRecordsAsync();
let delete_array = [];
table_data.records.find(record => {if (record.getCellValue('is spare teacher?') == true){delete_array.push(record.id)}});
while (delete_array.length > 0) { await table.deleteRecordsAsync(delete_array.slice(0, 50));
delete_array = delete_array.slice(50);};

Understanding Code:

In the above code, first, we are declaring a variable called 'table' and select 'Teachers' table. In the next line of code, we are declaring the 'table_data' variable and storing data of the 'Teachers' table. Then we are declaring an array named 'delete_array' and go through a loop we are storing all the ids of records that satisfy our condition. And finally, we are going through a loop and delete records.

There are two functions available for the delete operations just like the other operations.

  1. deleteRecordAsync()
  2. deleteRecordsAsync()

The first function is used for single record delete operation and the other one is used for multiple records delete operation. For this example, I have used a second function that is used to delete multiple records from the table. However both functions are almost the same, the only difference is that you pass multiple ids in the second function to delete multiple records and pass a single id in the first function to delete a single record.

So that is how you can delete records in airtable scripting app. Stay connected for more learning articles in this series.

Feel free to contact me for Airtable consulting services by CLICKING HERE.

monino solutions logo

COPYRIGHT © 2024 Monino Solutions.