Help

Cannot programmatically delete records in airtable

Topic Labels: Extensions
Solved
Jump to Solution
3305 13
cancel
Showing results for 
Search instead for 
Did you mean: 
Fredrick_Esedo
5 - Automation Enthusiast
5 - Automation Enthusiast

I have table with 3 fields name (lastname,firstname, email). id seems to be auto incremented in airtable.

Now am trying to programmatically delete records but its not working

here is the code

//table.deleteRecordAsync(record);
//table.deleteRecordsAsync([record1, record2]);

1.) delete records where id is = 1

   import {initializeBlock, useBase, useRecords, createRecordAsync, updateRecordAsync, updateRecordsAsync, table} from '@airtable/blocks/ui';
   import React from 'react';
    import {base} from '@airtable/blocks';
    const first_tab = base.getTableByNameIfExists('myfirst_table');
    const ok = first_tab.deleteRecordsAsync({id: '1', fields: {'lastname': 'Carrots-2','firstname': 'joy-2', 'email': 'joy-2@gmail.com'}});
    if(ok){
    alert('records Deleted successfully');
    }

2.) delete record where id is = 1 and email is = john@gmail.com

const first_tab2= base.getTableByNameIfExists('second_table');
    const ok = first_tab2.deleteRecordsAsync({id: '1', 'email': 'john@gmail.com', fields: {'lastname': 'Carrots-2','firstname': 'joy-2', 'email': 'joy-2@gmail.com'}});
    if(ok){
    alert('records deleted successfully');
    }

The codes is not working can someone help me out. Thanks

1 Solution

Accepted Solutions

While record.id is proper, record.lastname is not. When you’re trying to get the value of a field from a particular record, it would be record.getCellValueAsString('lastname'). The same page I linked earlier lists all the attributes (members) of a record you can reach immediately with a .

The reason your alert only shows the record ids for that table is because that’s all the information you passed to the alert. useRecords() results in an array of record IDs by default , and that’s what you passed to your alert().

See Solution in Thread

13 Replies 13
Matthew_Thomas
7 - App Architect
7 - App Architect

You are correct in that the record ID is randomly generated by Airtable and cannot be controlled during creation or update. Therefore, you wouldn’t be able to delete a record with ID “1”, because such a record would not exist. All Airtable records follow the pattern rec**************.

To programmatically delete any records, you would first need to fetch the records and then pass them in to table.deleteRecordsAsync().


If this answers your question, please consider marking it as “solution”. If not, I’m happy to work with you further. Thanks!

To expand upon Matthew’s answer, .deleteRecordAsync() and .deleteRecordsAsync() only ask which record(s) to delete, meaning the RecordID or the record object. It doesn’t look like you’re querying records within first_tab to find records within it to match your criteria.

Let’s look at your second example.

If you’re trying to find all records where {email} = ‘john@gmail.com’, why have then defined a single object of field values where the value for the email is ‘joy-2@gmail.com’? You don’t need fields:{anything} inside .deleteRecordsAsync() .

I recommend you take some time to review the custom blocks examples.

For instance, this example shows how you can properly filter records in a table to get the one you want to update/delete. Get the table (which you’ve done, its your variable first_tab or first_tab2), then get the records (const records = useRecords(first_tab), then filter those records (const recordsToUpdate = records.filter(record => record.id == 'the id of the record goes here';), then finally you should have first_tab.deleteRecordsAsync(recordsToUpdate)

@Matthew_Thomas
thanks for responding. I have tried as you suggested via code below but its showing error

const first_tab = base.getTableByNameIfExists(‘myfirst_table’);

const records = useRecords(first_tab);
const recordsToDelete = records.filter(record => record.id == '1');
const ok = first_tab.deleteRecordsAsync(recordsToDelete);
if(ok){
alert('records Deleted successfully');
}

This error it displayed

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. 
This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

@Kamille_Parks
thanks for responding. I have tried as you suggested via code below but its showing error

const first_tab = base.getTableByNameIfExists(‘myfirst_table’);

const records = useRecords(first_tab);
const recordsToDelete = records.filter(record => record.id == '1');
const ok = first_tab.deleteRecordsAsync(recordsToDelete);
if(ok){
alert('records Deleted successfully');
}

This error it displayed

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. 
This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

Well for starters, as Matthew explained, record.id cannot equal 1. All airtable record IDs are 17 characters long and always begin with rec.

If you are trying to reference a field that you made called id, you’ll need .getCellValue or .getCellValueAsString as explained here. This most likely won’t fix your error. I’d check running first_tab.delete....() without making it a variable (remove const ok =) to see if the record actually deletes itself.

Also, you don’t need to continue double-posting replies. Just tag both people in the same comment.

@Kamille_Parks
I have done as you suggested but the error persist. Its this line of code that causes the error of invalid hook call… I outlined above. may be something is missing there

const records = useRecords(first_tab);

Have you imported useRecords()? import {all the stuff you've already imported from blocks/ui, useRecords} from '@airtable/blocks/ui';

@Kamille_Parks thanks for your amazing contributions so far.

I have added all the stuff and the error is gone but the record is not deleted yet. I tried also deleting with lastname but no way. you mention of .getCellValueAsString please where do I added it. thanks

const first_tab= base.getTableByName('myfirst_table');
const records = useRecords(first_tab);

alert(records);
const recordsToDelete = records.filter(record => record.lastname == 'esedo');

 table.deleteRecordsAsync(recordsToDelete);

when I alert records, it seems to be getting only the record id and no lastname. This is what it alerted.

[Record recuIVFFOmKmtzWkP],[Record recuH77MnoPVCPfE9],[Record recvJDIU3Yvkd4pM9],[Record rec1Tj4f6RUioCiSf],[Record reclfWmjQ4za1qKfo]

While record.id is proper, record.lastname is not. When you’re trying to get the value of a field from a particular record, it would be record.getCellValueAsString('lastname'). The same page I linked earlier lists all the attributes (members) of a record you can reach immediately with a .

The reason your alert only shows the record ids for that table is because that’s all the information you passed to the alert. useRecords() results in an array of record IDs by default , and that’s what you passed to your alert().

Thanks. its working now. you are the best in the world Mr. Kamille.

I try updating now as I posted here and see how it goes.

Thank you

Glad everything’s working.

(Also I’m a woman lol)

Thank You Lady/Madam Kamille

@Kamille_Parks Please My record updates works with id but does not work with lastname. please help. Issues with updating records

Thanks