0

I have an idea firestore collection where documents of different users are stored, how can I select all documents of a specific user and delete?

I need to add delete account button, after pressed, all information of users was deleted.

FirebaseFirestore.instance.collection('ideas').get().then(
            (value) {
              value.docs.forEach((result) {
                print(result.data()[email!]);
              });
            },
          );

1 Answers1

0

This depends on how an idea document is associate with a specific user. If you have field (say user) in the document with the UID of the user whose idea it is, you can use a query to get the documents for a specific user with:

// Create a reference to the cities collection
final ideasRef = db.collection("ideas");

// Create a query against the collection.
final query = ideasRef.where("user", isEqualTo: "uidOfTheUser");

And then you execute the query to get its results.

To delete these documents, also see my answer here: How to delete Documents by Value name?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I have a user UID, in this case, will the above code work if there are many documents of different users in the collection and one user can have 5 documents, will the above code delete all 5 documents? – OverCome231 Dec 06 '22 at 17:15
  • The code I shared deletes noting yet, but merely uses a query to get the ideas for a user. To delete documents see https://firebase.google.com/docs/firestore/manage-data/delete-data, and to delete documents based on a query see: https://stackoverflow.com/search?q=%5Bgoogle-cloud-firestore%5D%5Bflutter%5D+query+delete+user%3Ame – Frank van Puffelen Dec 06 '22 at 17:33