Back

All documents not getting deleted

  • 0
  • Functions
  • Cloud
Chandan Gowda
27 Mar, 2024, 12:28

I'm calling this from a cloud function, deletes a few documents. But not all of them. Why ?

def delete_all_documents(database, collection_id): docsRef = database.list_documents(MASTER_DATABASE_ID,collection_id) for doc in docsRef["documents"]: database.delete_document(MASTER_DATABASE_ID, collection_id, doc["$id"])

TL;DR
Issue: Developers are experiencing trouble deleting all documents from a collection in Python and TypeScript. Solution: For Python, modify the delete_all_documents function without queries to ensure all documents are deleted. For TypeScript, update the cleanCollection function to remove the query limitations and it should work.
Ryan
27 Mar, 2024, 12:31

Are you using any queries when using list_documents? If I remember correctly, it defaults to a limit of 25 when a limit has not been set using queries

Chandan Gowda
27 Mar, 2024, 13:41

No queries

Chandan Gowda
27 Mar, 2024, 13:41

list all douments

Kenny
27 Mar, 2024, 13:43

You'll need to implement something like this.

TypeScript
  async cleanCollection(databaseId, collectionId) {
    let response;
    const queries = [
      Query.lessThan('$createdAt', getExpiryDate()),
      Query.limit(25),
    ];
    do {
      response = await this.databases.listDocuments(
        databaseId,
        collectionId,
        queries
      );
      await Promise.all(
        response.documents.map((document) =>
          this.databases.deleteDocument(databaseId, collectionId, document.$id)
        )
      );
    } while (response.documents.length > 0);
  }

This function specifically deletes items after x date, but it should be easy to just update it to delete everything in a collection.

Chandan Gowda
27 Mar, 2024, 13:52

Thanks!

Chandan Gowda
27 Mar, 2024, 13:52

So this should work on python right ?

Chandan Gowda
27 Mar, 2024, 13:52
TypeScript
def delete_all_documents(database, collection_id):
    docsRef = database.list_documents(MASTER_DATABASE_ID,collection_id)
    while docsRef["total"]>0:
      for doc in docsRef["documents"]:
          database.delete_document(MASTER_DATABASE_ID, collection_id, doc["$id"])
      docsRef = database.list_documents(MASTER_DATABASE_ID,collection_id)
Kenny
27 Mar, 2024, 13:56

Without any experience in Python it's hard to tell you with confidence it will work, maybe someone with more knowledge there can jump in.

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