What if there are lakhs of documents in a collection. Is there a way to delete all of them using a cloud function with python runtime ?
Yes, you can delete all documents in an Appwrite collection using a cloud function with the Python runtime. Here's a high-level overview of the steps involved:
- Initialize the Appwrite Client: You need to initialize the Appwrite Client in your Python code. This involves setting the endpoint, project ID, and API key.
- List Documents: Use the listDocuments method from the Databases service to retrieve all the documents in the collection.
- Delete Documents: Iterate through the list of documents and delete each one using the deleteDocument method from the Databases service.
Here's a sample Python code snippet to achieve this:
from appwrite.client import Client
from appwrite.services.databases import Databases
client = Client()
client.set_endpoint('https://cloud.appwrite.io/v1')
client.set_project('<YOUR_PROJECT_ID>')
client.set_key('<YOUR_API_KEY>')
databases = Databases(client)
# List documents
documents = databases.list_documents('<DATABASE_ID>', '<COLLECTION_ID>')['documents']
# Delete documents
for document in documents:
databases.delete_document('<DATABASE_ID>', '<COLLECTION_ID>', document['$id'])
Please replace <YOUR_PROJECT_ID>, <YOUR_API_KEY>, <DATABASE_ID>, and <COLLECTION_ID> with your actual project and collection details.
For more detailed information, you can refer to the Appwrite documentation on Cloud Functions and Python Quick Starts.
hello ji ye lo ap ka solution
Thank you so much. Are you sure this is the most efficient solution? Are there any alternatives ? @Drake
I dont know. I searched on google and found this
Thanks anyways @Ryan
No problem
This looks like it was written with ChatGPT 😅
nah, Perplexity AI 😎
it is ChatGPT but with internet access
Right - because ChatGPT wouldn’t know about Appwrite. But there’s also Bing chat, which is ChatGPT with Internet access - and I think if you pay for GPT-4, you’ll also get Internet?
yes, that is correct. And ChatGPT knows about Appwrite, but very limited info. and idk why but I only use bing chat for the free image gen and I aint payin for GPT-4
@Drake I used this method but when there were many documents, some of the documents didnt get deleted.
This is an old function but the logic should be fine. This does what you're wanting and should delete all documents in a collection. I know it's node but it shouldn't be too difficult to translate that logic to python. If you need any help lmk.
@Kenny Dont you think the function's timeout has anythimng to do with this ?
It could depending on how many documents you have, you can always increase the timeout?
Please don't tag people as it can be very disruptive. You have plenty of community members here answering you already
Okayy
Recommended threads
- Type Mismatch in AppwriteException
There is a discrepancy in the TypeScript type definitions for AppwriteException. The response property is defined as a string in the type definitions, but in pr...
- What Query's are valid for GetDocument?
Documentation shows that Queries are valid here, but doesn't explain which queries are valid. At first I presumed this to be a bug, but before creating a githu...
- HTTP POST to function returning "No Appw...
Hi everyone, I’m running into an issue with my self-hosted Appwrite instance. I’ve set up my environment variables (APPWRITE_FUNCTION_PROJECT_ID, APPWRITE_FUNC...