#community-help

Deleting Records Not in List with Python and Filters

TLDR Mojan needed help deleting records not in a list using filters. Gustavo suggested using filter_by and Mojan successfully implemented the solution using Python.

Powered by Struct AI

1

8
3mo
Solved
Join the chat
Jun 20, 2023 (3 months ago)
Mojan
Photo of md5-813f8f14e16940e952eb3fe25d0c4523
Mojan
10:47 PM
I'm trying to delete all records in my index that don't exist in a particular list. Not sure how I would put that condition in there
Gustavo
Photo of md5-f930fdb99fd46477205fa1201164ea50
Gustavo
10:53 PM
I believe filter_by=id:!=[id1,id2,id3] does what you need, but try it with caution, I'm not 100% sure
Mojan
Photo of md5-813f8f14e16940e952eb3fe25d0c4523
Mojan
11:17 PM
So the list is made dynamically and has a name , let's say doc_ids . Do you know how I would place the list name in there ? Because, I can't certainly put that inside quotes.
Do you know how I can do that without quotes ?
Gustavo
Photo of md5-f930fdb99fd46477205fa1201164ea50
Gustavo
11:31 PM
I guess it depends on the language
Mojan
Photo of md5-813f8f14e16940e952eb3fe25d0c4523
Mojan
11:36 PM
Python. I tried :

 {'filter_by': f"doc_id: != {doc_ids}"}

and it worked !
Thank you for your help :)

1

Jun 21, 2023 (3 months ago)
Ed
Photo of md5-120c789e9edae8b90bf59cf0e2612b66
Ed
09:02 AM
do you midn sharing the whole query?
09:02
Ed
09:02 AM
Mojan
Mojan
Photo of md5-813f8f14e16940e952eb3fe25d0c4523
Mojan
09:12 PM
I don't have a whole query so to speak. I have a function, which deletes the records. You feed it a list of ids and it deletes all the records in the index that don't exist in that list :

client.collections[table_name].documents.delete({'filter_by': f"{col_name}: != {id_list}"})

table_name is the name of the db table which is the same as index name here, column_name is the name of the field , which in this case is doc_id and id_list is the list which is being fed to the function .