diff --git a/firestore/samples/pom.xml b/firestore/samples/pom.xml new file mode 100644 index 00000000000..978b9a5e01b --- /dev/null +++ b/firestore/samples/pom.xml @@ -0,0 +1,72 @@ + + + + 4.0.0 + com.example.firestore + firestore-samples + 1.0-SNAPSHOT + + + + shared-configuration + com.google.cloud.samples + 1.2.2 + + + + 21 + 21 + + + + + + libraries-bom + com.google.cloud + import + pom + 26.80.0 + + + + + + + com.google.cloud + google-cloud-firestore + + + + + truth + com.google.truth + test + 1.4.5 + + + org.junit.jupiter + junit-jupiter + 5.14.3 + test + + + diff --git a/firestore/samples/src/main/java/com/example/firestore/DeleteCollection.java b/firestore/samples/src/main/java/com/example/firestore/DeleteCollection.java new file mode 100644 index 00000000000..b1c79790786 --- /dev/null +++ b/firestore/samples/src/main/java/com/example/firestore/DeleteCollection.java @@ -0,0 +1,54 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.firestore; + +// [START firestore_data_delete_collection] + +import com.google.api.core.ApiFuture; +import com.google.cloud.firestore.CollectionReference; +import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.FirestoreOptions; + +public class DeleteCollection { + + /** + * Delete a collection and all its subcollections. + * + * @param projectId The Google Cloud project ID + * @param collectionName The name of the collection to delete + */ + public static void deleteCollection(String projectId, String collectionName) throws Exception { + FirestoreOptions firestoreOptions = + FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build(); + try (Firestore db = firestoreOptions.getService()) { + CollectionReference collection = db.collection(collectionName); + + ApiFuture future = db.recursiveDelete(collection); + + future.get(); + System.out.println("Collection and all its subcollections deleted successfully."); + } + } + + public static void main(String[] args) throws Exception { + String projectId = "example-project-id"; + String collectionName = "example-collection-name"; + + deleteCollection(projectId, collectionName); + } +} +// [END firestore_data_delete_collection] diff --git a/firestore/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java b/firestore/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java new file mode 100644 index 00000000000..143a4aff3c5 --- /dev/null +++ b/firestore/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java @@ -0,0 +1,69 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.firestore; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class DeleteCollectionIT { + + private static ByteArrayOutputStream bout; + private static PrintStream prevPrintStream; + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String COLLECTION_NAME = "usa-cities"; + private static final String DOCUMENT_NAME = "LA"; + private static final String NAME = "Los Angeles"; + + private static String getEnvVar(String varName) { + String value = System.getenv(varName); + assertNotNull( + String.format("Environment variable '%s' must be set to perform these tests.", varName), + value); + return value; + } + + @BeforeAll + public static void setUp() throws Exception { + getEnvVar("GOOGLE_CLOUD_PROJECT"); + + // Create collection + Utils.createCollection(PROJECT_ID, COLLECTION_NAME, DOCUMENT_NAME, NAME); + + prevPrintStream = System.out; + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @AfterAll + public static void tearDown() { + System.setOut(prevPrintStream); + } + + @Test + public void testDeleteCollection() throws Exception { + DeleteCollection.deleteCollection(PROJECT_ID, COLLECTION_NAME); + + String output = bout.toString(); + assertTrue(output.contains("Collection and all its subcollections deleted successfully.")); + } +} diff --git a/firestore/samples/src/test/java/com/example/firestore/Utils.java b/firestore/samples/src/test/java/com/example/firestore/Utils.java new file mode 100644 index 00000000000..3caffc7f5ec --- /dev/null +++ b/firestore/samples/src/test/java/com/example/firestore/Utils.java @@ -0,0 +1,41 @@ +/* + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.firestore; + +import com.google.api.core.ApiFuture; +import com.google.cloud.firestore.Firestore; +import com.google.cloud.firestore.FirestoreOptions; +import com.google.cloud.firestore.WriteResult; +import java.util.HashMap; +import java.util.Map; + +public class Utils { + public static void createCollection( + String projectId, String collectionName, String documentName, String name) throws Exception { + // Create collection and document to be deleted + FirestoreOptions firestoreOptions = + FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build(); + try (Firestore db = firestoreOptions.getService()) { + + Map docData = new HashMap<>(); + docData.put("name", name); + ApiFuture future = + db.collection(collectionName).document(documentName).set(docData); + future.get(); + } + } +}