Deta Base Delete using filter query. #236
-
Acc. to docs delete(key: str) But what if I need to delete all the records having the same deta_task_db.delete( {"user_id": "Harshit" } ) Can I do that? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Deta currently doesn't offer a What you can do for your example is fetch all records with the same records = db.fetch({ "user_id": "Harshit" })
for record in records:
db.delete(record.key) Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
However if you have the list of keys to be deleted then you can do delete request asynchronously to the api which will be faster. example: import aiohttp
import asyncio
async def del_key(session, url):
async with session.delete(url) as resp:
return resp.status
async def main():
async with aiohttp.ClientSession() as session:
tasks = []
for key in target_keys:
url = f'https://database.deta.sh/v1/{project_id}/{base_name}/items/{key}'
tasks.append(asyncio.create_task(del_key(session, url)))
done = await asyncio.gather(*tasks)
for status_code in done:
print(status_code)
asyncio.run(main()) ** |
Beta Was this translation helpful? Give feedback.
Hi @harshitsinghai77,
Deta currently doesn't offer a
deleteMany
method, so you need to manually delete each item.What you can do for your example is fetch all records with the same
user_id
and then loop through the returned records and delete each one individually by their key:Hope this helps!