8000 🚀 Feature: Bulk Delete documents in a collection · Issue #3520 · appwrite/appwrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

🚀 Feature: Bulk Delete documents in a collection #3520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 tasks done
skyface753 opened this issue Jul 8, 2022 · 14 comments
Open
2 tasks done

🚀 Feature: Bulk Delete documents in a collection #3520

skyface753 opened this issue Jul 8, 2022 · 14 comments
Labels
enhancement New feature or request product / databases Fixes and upgrades for the Appwrite Database.

Comments

@skyface753
Copy link

🔖 Feature description

Hello,

maybe a button, which deletes all documents in the collection would be pretty nice.

Thanks in advance :D

🎤 Pitch

In my case, I currently getting started with appwrite and building my first project.
Therefore I create many dummy documents and have to delete them all manually.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

@hey-nicolasklein
Copy link

I would also like a route to delete documents by a query. For example, all documents having a certain field set to the same value.

This could work similar to fetching documents based on a query.

@frmatthew
Copy link

This feature would be very helpful, especially during development. Right now it's tedious to delete all documents in a collection, but this happens a lot during various development phases.

@stnguyen90
Copy link
Contributor

This feature would be very helpful, especially during development. Right now it's tedious to delete all documents in a collection, but this happens a lot during various development phases.

As a workaround right now, I recommend using the Appwrite CLI to maintain your Collections. You can, then, delete your collection to delete everything and then use the Appwrite CLI to deploy your collection to set up all the attributes and indexes.

@htetlynnhtun
Copy link

We really need this.

@barart
Copy link
barart commented Jan 12, 2023

+1 on this

@DennisFeldbusch
Copy link

Would love this feature, too.

@stnguyen90 stnguyen90 added the product / databases Fixes and upgrades for the Appwrite Database. label Jan 26, 2023
@stnguyen90
Copy link
Contributor

Another way to wipe a collection is with an Appwrite Function: Wipe an Appwrite Collection.

@DennisFeldbusch
Copy link
DennisFeldbusch commented Jan 26, 2023

Another way to wipe a collection is with an Appwrite Function: Wipe an Appwrite Collection.

This is a working workaroun 8000 d but not an elegant way because:

  1. If I only want to delete a bunch of documents I would delete them all and would have to restore the documents I want to keep
  2. It creates a lot of overhead due to deleting and again building a collection over and over. Permission set only in the Frontend for testing purposes have to be set there again.

A simple simple checkbox for all the documents would be nice in order to select the ones to delete.

@mahamat-ali
Copy link

This is a utility function that helps deletes all documents in a collection without deleting the collection itself (it means attributes, permissions, etc..)

Future<bool> deleteAllDocuments({
  required String databaseId,
  required String collectionId,
}) async {
  Client client = Client();
  client
      .setEndpoint(AppwriteConstants.endPoint)
      .setProject(AppwriteConstants.projectId)
      .setSelfSigned(status: true);
  Databases db = Databases(client);

  try {
    final docs = await db.listDocuments(
      databaseId: databaseId,
      collectionId: collectionId,
    );
    for (var doc in docs.documents) {
      final id = doc.data['\$id'];
      await db.deleteDocument(
        databaseId: databaseId,
        collectionId: collectionId,
        documentId: id,
      );
    }
    return true;
  } on AppwriteException catch (e) {
    print(e.message ?? 'Some error ocurred while deleting documents.');
    return false;
  } catch (e) {
    print(e.toString());
    return false;
  }
}

@pratikb64
Copy link

This is a utility function that helps deletes all documents in a collection without deleting the collection itself (it means attributes, permissions, etc..)

Future<bool> deleteAllDocuments({
  required String databaseId,
  required String collectionId,
}) async {
  Client client = Client();
  client
      .setEndpoint(AppwriteConstants.endPoint)
      .setProject(AppwriteConstants.projectId)
      .setSelfSigned(status: true);
  Databases db = Databases(client);

  try {
    final docs = await db.listDocuments(
      databaseId: databaseId,
      collectionId: collectionId,
    );
    for (var doc in docs.documents) {
      final id = doc.data['\$id'];
      await db.deleteDocument(
        databaseId: databaseId,
        collectionId: collectionId,
        documentId: id,
      );
    }
    return true;
  } on AppwriteException catch (e) {
    print(e.message ?? 'Some error ocurred while deleting documents.');
    return false;
  } catch (e) {
    print(e.toString());
    return false;
  }
}

This works but API is rate limited 60 requests every 1 minutes per IP address

@ashuvssut
Copy link
ashuvssut commented Jun 12, 2023

You can delete using your own server or appwrite serveless functions

Here is the server code that I use to delete all documents in the collection recursively. I have observed that not all docs get deleted when executed a function to delete All docs, so I created this recursive function which does the job to delete all docs

I have vercel serverless API endpoints written in TypeScript which delete these docs. Here's the code:-

// api/deleteAllDocs/route.ts

import { NextResponse } from "next/server";
import { Client, Databases } from "node-appwrite";

export async function deleteDocs(
  db: Databases,
  dbId: string,
  collId: string,
  pass = 1,
) {
  if (pass > 10) throw new Error("10 passes reached");

  const docListObj = await db.listDocuments(dbId, collId);

  const docList = docListObj.documents;

  docList.forEach(async doc => await db.deleteDocument(dbId, collId, doc.$id));

  const docListObjAgain = await db.listDocuments(dbId, collId);
  let lastPass: number;
  if (docListObjAgain.documents.length > 0) {
    lastPass = await deleteDocs(db, dbId, collId, pass + 1);
  } else {
    console.log(`deleteDocs() pass count: ${pass}`);
    return pass;
  }

  return lastPass;
}

export async function POST() {   //----------------- START---------------------------------//
  const client = new Client()
    .setEndpoint(process.env.APPWRITE_ENDPOINT!)
    .setProject(process.env.APPWRITE_PROJECT_ID!)
    .setKey(process.env.APPWRITE_SERVER_KEY!);

  const db = new Databases(client);

  try {
    const passes = await deleteDocs(
      db,
      process.env.APPWRITE_DATABASE_ID!,
      process.env.APPWRITE_COLLECTION_ID!,
    );

    return NextResponse.json({ success: true, passes });
  } catch (e: any) {
    console.error(e);
    return NextResponse.json({ error: e.message });
  }
}

I use my bash curl command to hit this endpoint:-
curl --location --request POST 'http://localhost:2023/api/deleteAllDocs'

@lars285
Copy link
lars285 commented Jun 12, 2024

I need this feature. 🚀

@JannikEngel
Copy link

The feature would change my life 🚀

@stnguyen90 stnguyen90 changed the title 🚀 Feature: Delete all documents in a collection 🚀 Feature: Bulk Delete documents in a collection Oct 18, 2024

908C
@eldadfux
Copy link
Member

This is available on the Appwrite console. We will be exploring ways to add an API ability to do the same.Image

@eldadfux eldadfux removed the product / console Console, UI and UX issues label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request product / databases Fixes and upgrades for the Appwrite Database.
Projects
None yet
Development

No branches or pull requests

0