Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion samples/snippets/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>26.65.0</version>
<version>26.75.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.google.cloud.firestore.FieldValue;
import com.google.cloud.firestore.Firestore;
import com.google.cloud.firestore.Query;
import com.google.cloud.firestore.QueryDocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.firestore.SetOptions;
import com.google.cloud.firestore.TransactionOptions;
Expand Down Expand Up @@ -304,27 +303,16 @@ void deleteDocument() throws Exception {
}

// [START firestore_data_delete_collection]
/**
* Delete a collection in batches to avoid out-of-memory errors. Batch size may be tuned based on
* document size (atmost 1MB) and application requirements.
*/
void deleteCollection(CollectionReference collection, int batchSize) {
/** Delete a collection and all its subcollections. */
void deleteCollection(CollectionReference collection) {
Firestore db = collection.getFirestore();

ApiFuture<Void> future = db.recursiveDelete(collection);
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) {
document.getReference().delete();
++deleted;
}
if (deleted >= batchSize) {
// retrieve and delete another batch
deleteCollection(collection, batchSize);
}
future.get();
System.out.println("Collection and all its subcollections deleted successfully.");
} catch (Exception e) {
System.err.println("Error deleting collection : " + e.getMessage());
System.err.println("Error deleting collection recursively : " + e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ protected static void deleteAllDocuments(Firestore db) throws Exception {
}
}

protected static void deleteCollection(Firestore db, String collectionName) throws Exception {
ApiFuture<Void> future = db.recursiveDelete(db.collection(collectionName));
try {
future.get();
System.out.println("Collection and all its subcollections deleted successfully.");
} catch (Exception e) {
System.err.println("Error deleting collection recursively : " + e.getMessage());
}
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
db.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import com.google.cloud.firestore.CollectionReference;
import com.google.cloud.firestore.DocumentReference;
import com.google.cloud.firestore.DocumentSnapshot;
import com.google.cloud.firestore.QuerySnapshot;
import com.google.cloud.firestore.WriteResult;

import java.util.Date;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -152,6 +155,23 @@ public void testDeleteDocument() throws Exception {
getDocumentDataAsMap(db.collection("cities").document("DC"));
}

@Test
public void testDeleteCollection() throws Exception {
// Create collection and document to be deleted
Map<String, Object> docData = Map.of("name", "Los Angeles");
ApiFuture<WriteResult> future = db.collection("usa-cities").document("LA").set(docData);
future.get();

// Delete collection
manageDataSnippets.deleteCollection(db.collection("usa-cities"));

// Verify collection is deleted by trying to read from it
CollectionReference collectionReference = db.collection("usa-cities");
collectionReference.limit(1);
QuerySnapshot snapshot = collectionReference.get().get();
assertTrue(snapshot.isEmpty());
}

@Test
public void testSimpleTransaction() throws Exception {
DocumentReference docRef = db.collection("cities").document("SF");
Expand Down Expand Up @@ -208,8 +228,8 @@ public void testSnapshotReads() throws Exception {

@AfterClass
public static void tearDown() throws Exception {
manageDataSnippets.deleteCollection(db.collection("cities"), 10);
manageDataSnippets.deleteCollection(db.collection("users"), 10);
deleteCollection(db, "cities");
deleteCollection(db, "users");
manageDataSnippets.close();
}
}
Loading