Skip to main content
Solved

Delete a record by automation


  • New Participant
  • 4 replies

I am looking to Run a script in an Action. basically whenever the Category “Done” or “Postpone” is set to the record by single select field. I like to run a script that deletes those records. The trigger is pretty much easy to do by the automation configuration. The script is the one I didn’t figure out how to do.

Best answer by kuovonne

Thank you for the clarification that you wanted a free script.

Here is a simple script. You need to edit the script to match the name of your table.
You also need to create an input variable for the automation named recordId with the value of the record id of the triggering record from step 1.

let table = base.getTable("Table Name");
let inputConfig = input.config();
let recordId = inputConfig['recordId']
await table.deleteRecordAsync(recordId);
View original
Did this topic help you find an answer to your question?

13 replies

kuovonne
Forum|alt.badge.img+27
  • Brainy
  • 6002 replies
  • April 4, 2021

Welcome to the Airtable community!

You need to call deleteRecordAsync() on the table to delete the record.


  • Author
  • New Participant
  • 4 replies
  • April 5, 2021
kuovonne wrote:

Welcome to the Airtable community!

You need to call deleteRecordAsync() on the table to delete the record.


How do I write a script that will delete a record that has “Done” value in it ?
single select field with “Done”.


kuovonne
Forum|alt.badge.img+27
  • Brainy
  • 6002 replies
  • April 5, 2021
ido11 wrote:

How do I write a script that will delete a record that has “Done” value in it ?
single select field with “Done”.


Are you having trouble getting your script to work, or do you have trouble getting started?

If you have trouble getting you code to work, please post what you have and the community can help you debug.

If you do not know where to start, decide if you want to learn to write scripts, or if you just want working code.

If you want to learn to write scripts, tell us your current coding experience and we can point you to resources.

If you just want working code, please say if you are looking for someone to give you the script for free, or if you have budget to hire someone.


  • Author
  • New Participant
  • 4 replies
  • April 5, 2021
kuovonne wrote:

Are you having trouble getting your script to work, or do you have trouble getting started?

If you have trouble getting you code to work, please post what you have and the community can help you debug.

If you do not know where to start, decide if you want to learn to write scripts, or if you just want working code.

If you want to learn to write scripts, tell us your current coding experience and we can point you to resources.

If you just want working code, please say if you are looking for someone to give you the script for free, or if you have budget to hire someone.


Actually I have zero experience with coding.
whatever I try to do doesn’t work.

I would appreciate if someone could write the script.


kuovonne
Forum|alt.badge.img+27
  • Brainy
  • 6002 replies
  • April 5, 2021

Do you have budget to hire someone to write the script, or are you looking for a free script?


  • Author
  • New Participant
  • 4 replies
  • April 5, 2021
kuovonne wrote:

Do you have budget to hire someone to write the script, or are you looking for a free script?


I looking for a free one.


kuovonne
Forum|alt.badge.img+27
  • Brainy
  • 6002 replies
  • Answer
  • April 5, 2021

Thank you for the clarification that you wanted a free script.

Here is a simple script. You need to edit the script to match the name of your table.
You also need to create an input variable for the automation named recordId with the value of the record id of the triggering record from step 1.

let table = base.getTable("Table Name");
let inputConfig = input.config();
let recordId = inputConfig['recordId']
await table.deleteRecordAsync(recordId);

  • Author
  • New Participant
  • 4 replies
  • April 5, 2021
kuovonne wrote:

Thank you for the clarification that you wanted a free script.

Here is a simple script. You need to edit the script to match the name of your table.
You also need to create an input variable for the automation named recordId with the value of the record id of the triggering record from step 1.

let table = base.getTable("Table Name");
let inputConfig = input.config();
let recordId = inputConfig['recordId']
await table.deleteRecordAsync(recordId);

thank you so much.
It work after a little playing around.


Forum|alt.badge.img+1
  • New Participant
  • 1 reply
  • May 15, 2023

Hi team,
I'm getting the following error message when running the free script:

ERROR

TypeError: Invalid arguments passed to table.deleteRecordAsync(recordOrRecordId): • recordOrRecordId → recordOrRecordId should be a Record, not undefined or recordOrRecordId should be a string, not undefined
    at main on line 4


Can anyone advise?
Thanks


Forum|alt.badge.img+10
  • Participating Frequently
  • 19 replies
  • September 21, 2023
CC23 wrote:

Hi team,
I'm getting the following error message when running the free script:

ERROR

TypeError: Invalid arguments passed to table.deleteRecordAsync(recordOrRecordId): • recordOrRecordId → recordOrRecordId should be a Record, not undefined or recordOrRecordId should be a string, not undefined
    at main on line 4


Can anyone advise?
Thanks


Maybe you did not declare RecorId in the input.config() column?

 




Alexey_Gusev
Forum|alt.badge.img+23
CC23 wrote:

Hi team,
I'm getting the following error message when running the free script:

ERROR

TypeError: Invalid arguments passed to table.deleteRecordAsync(recordOrRecordId): • recordOrRecordId → recordOrRecordId should be a Record, not undefined or recordOrRecordId should be a string, not undefined
    at main on line 4


Can anyone advise?
Thanks


You didn't pass the record ID. Should be done in such way. Table name must be the same as in trigger
variable name ('id') - is that you must choose at the left side of code editor.

 



You should also understand that it doesn't remove current 'Done' records, it just affects new. Or you can remove 'Done' status (for many records as well) and then press Ctrl+Z. Status will be returned, but each of records will run automation to be deleted. Don't recommended to run for more than 50 at a time -  it can cause automation run delay and failed runs.


Forum|alt.badge.img+4
  • New Participant
  • 3 replies
  • October 28, 2024

FOR ANYONE LOOKING FOR A SOLUTION to deleting records for new form submissions. I found a solution on youtube (thanks Dan Leeman) (see screenshot for my flow) 

const table = base.getTable('TABLE NAME');
const recordsToDelete = input.config().recordIds;
table.deleteRecordsAsync(recordsToDelete);

HERE IS THE CATCH 
1.MAKE SURE you have a find record step, or any way to pull the ID you want
2. When you configure the config step the name needs to be 'recordIds' (didnt know that mattered) 

https://www.youtube.com/watch?v=hNsPTMe_pso 


Alexey_Gusev
Forum|alt.badge.img+23
  • Brainy
  • 1126 replies
  • October 28, 2024
brdemers wrote:

FOR ANYONE LOOKING FOR A SOLUTION to deleting records for new form submissions. I found a solution on youtube (thanks Dan Leeman) (see screenshot for my flow) 

const table = base.getTable('TABLE NAME');
const recordsToDelete = input.config().recordIds;
table.deleteRecordsAsync(recordsToDelete);

HERE IS THE CATCH 
1.MAKE SURE you have a find record step, or any way to pull the ID you want
2. When you configure the config step the name needs to be 'recordIds' (didnt know that mattered) 

https://www.youtube.com/watch?v=hNsPTMe_pso 


A few comments on it:
MAKE SURE you have a find record step, or any way to pull the ID you want - Find Records always returns ARRAY of IDs, not a single ID. Even when you set Max number = 1 , it returns ARRAY having 1 element. 
[ 'recABC1223xyxdef' ]. Airtable improved their automations - when it expects a single ID from you , and you put array (with a single value), it auto-converts and uses it as a single value.
Linking / Lookup field value is also always array.
Function deleteRecordsAsync expects ARRAY of IDs as input, so it's OK in this case.

When you configure the config step the name needs to be 'recordIds' (didnt know that mattered) 
You can use any name you want, like 'records_to_be_removed' or 'recs'. The only thing that matters - name XXXX on the left side of the editor window must match name in input.config().XXXX
when you have several variables on a left side, use ES6 syntax to assign them all in one step, like
{firstVar, second_variable, name, id, whatever} = input.config()

Regarding code, the line 

 

 

 

table.deleteRecordsAsync(recordsToDelete);

 

 

 

won't work for records number>50.
also, in order to proper asynchronous function work, you should run it with await.
without await, your example still working 'as is', but can cause problem being part of bigger solution.
correct call is 

 

 

 

await table.deleteRecordsAsync(recordsToDelete)

 

 

 

when your recordsToDelete length is > 50, you have to run in batches:

 

 

 

while (recordsToDelete.length > 0) { await peopleTable.deleteRecordsAsync(recordsToDelete.slice(0, 50)); recordsToDelete = recordsToDelete.slice(50); }

 

 

 

or, using 'more eco-friendly' splice, that cuts part of existing array and not 'throwing it away' but returns it as function output, and the fact about (recordsToDelete.length > 0) that ( 0 ) = false,  (not 0) = true, it can be written as:

 

 

 

while (recordsToDelete.length) await peopleTable.deleteRecordsAsync(recordsToDelete.splice(0, 50));

 

 

 

so, finally it can be written as

 

 

 

const table = base.getTable('TABLE NAME'); const recordsToDelete = input.config().recordIds; while (recordsToDelete.length) await table.deleteRecordsAsync(recordsToDelete.splice(0,50))

 

 

 

 

it's simple and clear but sometimes, when you are sure their number always below 50, you can use one-liner

 

 

 

await base.getTable('TABLE NAME').deleteRecordsAsync(input.config().recordIds)

 

 

 


Reply