我只是像下面这样修改了一些正式的示例片段
static void deleteCollection(CollectionReference collection, int batchSize) {
try {
// retrieve a small batch of documents to avoid out-of-memory errors
ApiFuture<QuerySnapshot> future = collection.limit(batchSize).get();
int deleted = 0;
// future.get() blocks on document retrieval
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
//delete subcollections
Iterable<CollectionReference> collections =
document.getReference().listCollections();
for (CollectionReference collRef : collections) {
deleteCollection(collRef, batchSize);
}
//delete document
document.getReference().delete();
++deleted;
}
if (deleted >= batchSize) {
// retrieve and delete another batch
deleteCollection(collection, batchSize);
}
} catch (Exception e) {
System.err.println("Error deleting collection : " + e.getMessage());
}
}但是这个代码对我不起作用。文档子集合保持原样。这里怎么了?谢谢你的回答
发布于 2022-10-12 02:39:57
只是为某人分享正确的代码
private static void deleteCollection(CollectionReference collection, int batchSize) {
try {
// retrieve a small batch of documents to avoid out-of-memory errors
ApiFuture<QuerySnapshot> future = collection.limit(batchSize).get();
int deleted = 0;
// future.get() blocks on document retrieval
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
for (QueryDocumentSnapshot document : documents) {
//delete document
document.getReference().delete();
++deleted;
//delete subcollections
Iterable<CollectionReference> collections =
document.getReference().listCollections();
for (CollectionReference collRef : collections) {
deleteCollection(collRef, batchSize);
}
}
if (deleted >= batchSize) {
// retrieve and delete another batch
deleteCollection(collection, batchSize);
}
} catch (Exception e) {
System.err.println("Error deleting collection : " + e.getMessage());
}
}https://stackoverflow.com/questions/74035696
复制相似问题