0

I have one function is to upload the excel file into the firebase cloud firestore collection. Before i insert the data , i should clear all the document in the cloud firestore collection first . Is there any function or method can clear all the collection or document in one shot ?

void _importData() async {
    for (var table in data.tables.keys) {
      for (var row in data.tables[table].rows) {
        print("$row");

        await databaseReference.collection("vehicles").add({
          //row[i] = the column sequence in the excel
          'Car_Make': row[3],
          'Company': row[4],
          'Holder_Type': row[2],
          'Holder_Name': row[1],
          'Vehicle_No': row[0],
        }).then((value) => print("Upload Complete"));
      }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
HAHAHAHA
  • 19
  • 2
  • 8

1 Answers1

1

Firestore does not support deleting a whole Collection. You will have to get all the documents and delete each of them individually.

You may try the following:

FirebaseFirestore.instance.collection('vehicles').snapshots().forEach((querySnapshot) {
  for (QueryDocumentSnapshot docSnapshot in querySnapshot.docs) {
    docSnapshot.reference.delete();
  }
});
Thierry
  • 7,775
  • 2
  • 15
  • 33
  • Undefined class 'QueryDocumentSnapshot'. Try changing the name to the name of an existing class, or creating a class with the name 'QueryDocumentSnapshot'. – HAHAHAHA Feb 03 '21 at 12:07
  • What Firebase packages do you have in your `pubspec.yaml`? – Thierry Feb 03 '21 at 12:21