Script to Delete Data

Hello,

Could someone help me with a script to delete data from a table passing 1 field as a parameter?

For example, I want to delete all records that were recorded yesterday, so I would pass yesterday’s date to the parameter and all data would be deleted.

Thanks

1 Like

Would you benefit from a “delete everything in ‘this view’” script? That way you could more easily filter records using Airtable’s native UI and have a dead-simple script like so:

let table = base.getTable("name of table")
let view = table.getView("name of view")

let query = await view.selectRecordsAsync()

let records = query.records
output.markdown(`## Deleting ${records.length} records...`)

let batchMax = 50

while (records.length > 0) {
    await table.deleteRecordsAsync(records.slice(0, batchMax))
    output.markdown(`✅ *${Math.min(records.length, batchMax)} records deleted...*`)
    records = records.slice(batchMax)
}

output.markdown(`## All records deleted!`)
4 Likes

And if you aren’t afraid of heart attacks, you can go for even more immediate convenience by replacing table/view names with

let table = base.getTable(cursor.activeTableId)
let view  = table.getView(cursor.activeViewId)

There, a script that deletes whatever so much as looks at you wrong without ever needing another touchup. :sweat_smile:

For a less yolo and more granular approach, the actual scripting app has all the examples you need. Just open it in the base you’re trying to modify and it will dynamically load up working code examples using your base/field/etc names.

Thanks for the replies.

Hello, how I could use Airtable’s native UI in this case ?

Welcome to the Airtable community!

You mention passing a date as a parameter. What is passing the date?

Are you using Scripting app and getting a date from a user?

Are you using a web hook automation and passing a date to the automation?

Are you using a scheduled automation script?

How much of the script do you have written already, and how familiar are you with both javaScriot and the Airtable scripting api?

This is my first experience with Airtable. My front-end is Adalo and Backend is Airtable.

When I say pass a date it would be the current month only.

In short, I need a script to be executed on the first day of each month to delete data from the previous month, for example on August 1st I would have to delete all records referring to the month of July.

I mean setting up a View that filters everything from last month:

Since you aren’t passing whole dates like 6/25/2021 and instead passing something I assume looks like June, add a Formula field that uses the following formula, and use it as the basis for your View’s filter.

IF(
   DATETIME_FORMAT({name of month field}, "MMMM") = DATETIME_FORMAT(DATEADD(TODAY(), -1, "month"), "MMMM"),
   "Last Month"
)

I recommend opening up the Airtable user interface and laying around with it to get familiar with Airtable.

You could have an automation that automatically runs on the first of the month and deleted all the records for the previous month with a script.

However I recommend getting more familiar with Airtable first. For example, do you really need to delete the records, or could you use a filtered view instead?

I prefer deleting to save space because the data volume will be quite large