Skip to content

Commit 2724879

Browse files
fix(firestore): use recursive delete for collection
Update delete collection sample to use recursiveDelete Create test for this function Update previous uses of deleteCollection
1 parent 1f66dca commit 2724879

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

samples/snippets/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<dependency>
3535
<groupId>com.google.cloud</groupId>
3636
<artifactId>libraries-bom</artifactId>
37-
<version>26.65.0</version>
37+
<version>26.75.0</version>
3838
<type>pom</type>
3939
<scope>import</scope>
4040
</dependency>

samples/snippets/src/main/java/com/example/firestore/snippets/ManageDataSnippets.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.google.cloud.firestore.FieldValue;
2525
import com.google.cloud.firestore.Firestore;
2626
import com.google.cloud.firestore.Query;
27-
import com.google.cloud.firestore.QueryDocumentSnapshot;
2827
import com.google.cloud.firestore.QuerySnapshot;
2928
import com.google.cloud.firestore.SetOptions;
3029
import com.google.cloud.firestore.TransactionOptions;
@@ -304,27 +303,16 @@ void deleteDocument() throws Exception {
304303
}
305304

306305
// [START firestore_data_delete_collection]
307-
/**
308-
* Delete a collection in batches to avoid out-of-memory errors. Batch size may be tuned based on
309-
* document size (atmost 1MB) and application requirements.
310-
*/
311-
void deleteCollection(CollectionReference collection, int batchSize) {
306+
/** Delete a collection and all its subcollections. */
307+
void deleteCollection(CollectionReference collection) {
308+
Firestore db = collection.getFirestore();
309+
310+
ApiFuture<Void> future = db.recursiveDelete(collection);
312311
try {
313-
// retrieve a small batch of documents to avoid out-of-memory errors
314-
ApiFuture<QuerySnapshot> future = collection.limit(batchSize).get();
315-
int deleted = 0;
316-
// future.get() blocks on document retrieval
317-
List<QueryDocumentSnapshot> documents = future.get().getDocuments();
318-
for (QueryDocumentSnapshot document : documents) {
319-
document.getReference().delete();
320-
++deleted;
321-
}
322-
if (deleted >= batchSize) {
323-
// retrieve and delete another batch
324-
deleteCollection(collection, batchSize);
325-
}
312+
future.get();
313+
System.out.println("Collection and all its subcollections deleted successfully.");
326314
} catch (Exception e) {
327-
System.err.println("Error deleting collection : " + e.getMessage());
315+
System.err.println("Error deleting collection recursively : " + e.getMessage());
328316
}
329317
}
330318

samples/snippets/src/test/java/com/example/firestore/BaseIntegrationTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ protected static void deleteAllDocuments(Firestore db) throws Exception {
8383
}
8484
}
8585

86+
protected static void deleteCollection(Firestore db, String collectionName) throws Exception {
87+
ApiFuture<Void> future = db.recursiveDelete(db.collection(collectionName));
88+
try {
89+
future.get();
90+
System.out.println("Collection and all its subcollections deleted successfully.");
91+
} catch (Exception e) {
92+
System.err.println("Error deleting collection recursively : " + e.getMessage());
93+
}
94+
}
95+
8696
@AfterClass
8797
public static void tearDownAfterClass() throws Exception {
8898
db.close();

samples/snippets/src/test/java/com/example/firestore/snippets/ManageDataSnippetsIT.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import com.google.cloud.firestore.CollectionReference;
2727
import com.google.cloud.firestore.DocumentReference;
2828
import com.google.cloud.firestore.DocumentSnapshot;
29+
import com.google.cloud.firestore.QuerySnapshot;
30+
import com.google.cloud.firestore.WriteResult;
31+
2932
import java.util.Date;
3033
import java.util.Map;
3134
import java.util.Objects;
@@ -152,6 +155,23 @@ public void testDeleteDocument() throws Exception {
152155
getDocumentDataAsMap(db.collection("cities").document("DC"));
153156
}
154157

158+
@Test
159+
public void testDeleteCollection() throws Exception {
160+
// Create collection and document to be deleted
161+
Map<String, Object> docData = Map.of("name", "Los Angeles");
162+
ApiFuture<WriteResult> future = db.collection("usa-cities").document("LA").set(docData);
163+
future.get();
164+
165+
// Delete collection
166+
manageDataSnippets.deleteCollection(db.collection("usa-cities"));
167+
168+
// Verify collection is deleted by trying to read from it
169+
CollectionReference collectionReference = db.collection("usa-cities");
170+
collectionReference.limit(1);
171+
QuerySnapshot snapshot = collectionReference.get().get();
172+
assertTrue(snapshot.isEmpty());
173+
}
174+
155175
@Test
156176
public void testSimpleTransaction() throws Exception {
157177
DocumentReference docRef = db.collection("cities").document("SF");
@@ -208,8 +228,8 @@ public void testSnapshotReads() throws Exception {
208228

209229
@AfterClass
210230
public static void tearDown() throws Exception {
211-
manageDataSnippets.deleteCollection(db.collection("cities"), 10);
212-
manageDataSnippets.deleteCollection(db.collection("users"), 10);
231+
deleteCollection(db, "cities");
232+
deleteCollection(db, "users");
213233
manageDataSnippets.close();
214234
}
215235
}

0 commit comments

Comments
 (0)