Back

Efficient way to delete all documents in a collection ?

  • 0
  • Databases
  • Functions
Chandan Gowda
26 Nov, 2023, 15:04

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 ?

TL;DR
To efficiently delete all documents in a collection using a cloud function with the Python runtime, follow these steps: 1. Initialize the Appwrite client by setting the endpoint, project ID, and API key. 2. Use the `listDocuments` method from the Databases service to retrieve all documents in the collection. 3. 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: ```python from appwrite.client import Client from appwrite.services.databases import Databases client = Client() client.set_endpoint('https://cloud.appwrite
Ryan
26 Nov, 2023, 15:07

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:

TypeScript
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.

Ryan
26 Nov, 2023, 15:07

hello ji ye lo ap ka solution

Chandan Gowda
26 Nov, 2023, 15:33

Thank you so much. Are you sure this is the most efficient solution? Are there any alternatives ? @Drake

Ryan
26 Nov, 2023, 15:33

I dont know. I searched on google and found this

Chandan Gowda
26 Nov, 2023, 15:35

Thanks anyways @Ryan

Ryan
26 Nov, 2023, 15:35

No problem

ideclon
26 Nov, 2023, 15:59

This looks like it was written with ChatGPT 😅

Ryan
26 Nov, 2023, 16:00

nah, Perplexity AI 😎

Ryan
26 Nov, 2023, 16:00

it is ChatGPT but with internet access

ideclon
26 Nov, 2023, 16:02

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?

Ryan
26 Nov, 2023, 16:03

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

Chandan Gowda
27 Nov, 2023, 14:34

@Drake I used this method but when there were many documents, some of the documents didnt get deleted.

Kenny
27 Nov, 2023, 15:15

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.

https://github.com/open-runtimes/examples/blob/92ea32bd0c405264c5c4956a7ea5f976846d2f56/node/wipe_appwrite_collection/index.js#L17C3-L44C4

Chandan Gowda
27 Nov, 2023, 15:36

@Kenny Dont you think the function's timeout has anythimng to do with this ?

Kenny
27 Nov, 2023, 15:37

It could depending on how many documents you have, you can always increase the timeout?

Drake
27 Nov, 2023, 20:30

Please don't tag people as it can be very disruptive. You have plenty of community members here answering you already

Chandan Gowda
28 Nov, 2023, 02:36

Okayy

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more