davidhamann/python-fmrest

Delete multiple records

Closed this issue · 4 comments

@davidhamann I love this fmrest. The only issue I'm having is deleting multiple records. You can't do:

df = server.get_records().to_df()
to_delete = df.loc[myfield.isnull(),'recordId']
###This next for loop will not work because recordId is updated on every 'POST'
for record_id in to_delete:
  server.delete_record(record_id)

But I think you can use delete request on multiple recordIds. I think that's possible because you can delete multiple records at once on the webd. I'm going to look at the API request on how WebD deletes multiple records tomorrow. But maybe you already know how to do this.

Thanks for the nice words and for posting your issue.

Unfortunately, the data api currently does not natively support deleting multiple records. WebDirect is a different technology, so not directly comparable.

However, firing multiple delete_record calls shouldn't be an issue. Maybe even deleting records via a relationship/portal should work for your case.

I don't quite understand what you mean by:

This next for loop will not work because recordId is updated on every 'POST'

If you can isolate your issue and put together a self-contained example I would be happy to take a closer look.


I also thought about adding a method to python-fmrest to give you the ability to pass a list of record IDs to delete. However, as the wrapper could also only put together multiple calls, it wouldn't be one transaction and thus have no one result. I will think about it later; maybe we can make something work that has an intuitive API.

Hi David. Here was the code. It sometimes can't find a record id even though I just pulled it right from the server and turned it into a pandas dataframe. Then it iterates through the dataframe and deletes records based on the dataframes recordId. Occasionally it will throw a recordId not found. I thought this might be due to the recordId "shifting" every time there is a deleted id. So you delete a record and then the next record, _i, might become _i - 1. Is there any validity to that thinking? Would the recordIds always be the same no matter how many you deleted in the same for loop?


fms = fmrest.Server(
    'https://myserver.edu',
    user='admin',
    password='pword',
    database='sl',
    layout='DNA',
    verify_ssl=False
    # if you are testing without cert/domain you may need the parameter verify_ssl=False here.
)

fms.login()

#This gets records right from the server
df = fms.get_records().to_df()

#Delete 100 random records
for record in df.sample(100).iterrows():
    print(record[1]['Name'],record[1]['recordId'])
    fms.delete_record(record[1]['recordId'])

>>> FMS got an error, no recordId found```


David, I can't replicate the error yet. You can close if invalid if you want. I'm doing a bunch of deletions this way and I'll let you know if I have any problems. I'm keeping track of every record deleted for the moment to make sure everything is okay. Sorry for the confusion.

Record IDs are unique per record, so there shouldn't be any changing/shifting going on. The only way to change/re-use them is when you create a clone of the database.

As for the error you are receiving, I couldn't reproduce the issue. Please do let me know if it comes up again in a reproduceable fashion or if you're getting more info. Thanks.