From 56b25cbf4bbe4d11f7f14aaafec01ccb39538406 Mon Sep 17 00:00:00 2001 From: FRANCISCO JAVIER ALARCON ESPARZA Date: Sat, 9 May 2026 00:09:48 +0000 Subject: [PATCH 1/4] feat(firebase): add firebase recursive delete sample --- firebase/samples/pom.xml | 72 +++++++++++++++++++ .../example/firebase/DeleteCollection.java | 50 +++++++++++++ .../example/firebase/DeleteCollectionIT.java | 68 ++++++++++++++++++ .../test/java/com/example/firebase/Utils.java | 38 ++++++++++ 4 files changed, 228 insertions(+) create mode 100644 firebase/samples/pom.xml create mode 100644 firebase/samples/src/main/java/com/example/firebase/DeleteCollection.java create mode 100644 firebase/samples/src/test/java/com/example/firebase/DeleteCollectionIT.java create mode 100644 firebase/samples/src/test/java/com/example/firebase/Utils.java diff --git a/firebase/samples/pom.xml b/firebase/samples/pom.xml new file mode 100644 index 00000000000..d8db420ff4b --- /dev/null +++ b/firebase/samples/pom.xml @@ -0,0 +1,72 @@ + + + + 4.0.0 + com.example.firebase + firebase-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/firebase/samples/src/main/java/com/example/firebase/DeleteCollection.java b/firebase/samples/src/main/java/com/example/firebase/DeleteCollection.java new file mode 100644 index 00000000000..13c98199af6 --- /dev/null +++ b/firebase/samples/src/main/java/com/example/firebase/DeleteCollection.java @@ -0,0 +1,50 @@ +/* + * 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.firebase; + +// [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; +import java.util.concurrent.ExecutionException; + +public class DeleteCollection { + + /** Delete a collection and all its subcollections. */ + public static void deleteCollection(String projectId, String collectionName) + throws ExecutionException, InterruptedException { + FirestoreOptions firestoreOptions = + FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build(); + 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 ExecutionException, InterruptedException { + String projectId = "your-project-id"; + String collectionName = "your-collection-name"; + + deleteCollection(projectId, collectionName); + } +} +// [END firestore_data_delete_collection] diff --git a/firebase/samples/src/test/java/com/example/firebase/DeleteCollectionIT.java b/firebase/samples/src/test/java/com/example/firebase/DeleteCollectionIT.java new file mode 100644 index 00000000000..a1bfbf7540f --- /dev/null +++ b/firebase/samples/src/test/java/com/example/firebase/DeleteCollectionIT.java @@ -0,0 +1,68 @@ +/* + * 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.firebase; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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 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); + + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @AfterAll + public static void tearDown() { + System.setOut(null); + } + + @Test + public void testDeleteCollection() throws ExecutionException, InterruptedException { + DeleteCollection.deleteCollection(PROJECT_ID, COLLECTION_NAME); + + String output = bout.toString(); + assertTrue(output.contains("Collection and all its subcollections deleted successfully.")); + } +} diff --git a/firebase/samples/src/test/java/com/example/firebase/Utils.java b/firebase/samples/src/test/java/com/example/firebase/Utils.java new file mode 100644 index 00000000000..1defb292abb --- /dev/null +++ b/firebase/samples/src/test/java/com/example/firebase/Utils.java @@ -0,0 +1,38 @@ +/* + * 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.firebase; + +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(); + Firestore db = firestoreOptions.getService(); + Map docData = new HashMap<>(); + docData.put("name", name); + ApiFuture future = db.collection(collectionName).document(documentName).set(docData); + future.get(); + } +} From 6c7ed96f63f1e9d7317fcfed5061bf521233e147 Mon Sep 17 00:00:00 2001 From: FRANCISCO JAVIER ALARCON ESPARZA Date: Sat, 9 May 2026 00:17:40 +0000 Subject: [PATCH 2/4] rename folder --- .../example/firestore/DeleteCollection.java | 50 +++++++++++++ .../example/firestore/DeleteCollectionIT.java | 68 ++++++++++++++++++ .../java/com/example/firestore/Utils.java | 38 ++++++++++ firestore/samples/pom.xml | 72 +++++++++++++++++++ .../example/firestore/DeleteCollection.java | 50 +++++++++++++ .../example/firestore/DeleteCollectionIT.java | 68 ++++++++++++++++++ .../java/com/example/firestore/Utils.java | 38 ++++++++++ 7 files changed, 384 insertions(+) create mode 100644 firebase/samples/src/main/java/com/example/firestore/DeleteCollection.java create mode 100644 firebase/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java create mode 100644 firebase/samples/src/test/java/com/example/firestore/Utils.java create mode 100644 firestore/samples/pom.xml create mode 100644 firestore/samples/src/main/java/com/example/firestore/DeleteCollection.java create mode 100644 firestore/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java create mode 100644 firestore/samples/src/test/java/com/example/firestore/Utils.java diff --git a/firebase/samples/src/main/java/com/example/firestore/DeleteCollection.java b/firebase/samples/src/main/java/com/example/firestore/DeleteCollection.java new file mode 100644 index 00000000000..13c98199af6 --- /dev/null +++ b/firebase/samples/src/main/java/com/example/firestore/DeleteCollection.java @@ -0,0 +1,50 @@ +/* + * 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.firebase; + +// [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; +import java.util.concurrent.ExecutionException; + +public class DeleteCollection { + + /** Delete a collection and all its subcollections. */ + public static void deleteCollection(String projectId, String collectionName) + throws ExecutionException, InterruptedException { + FirestoreOptions firestoreOptions = + FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build(); + 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 ExecutionException, InterruptedException { + String projectId = "your-project-id"; + String collectionName = "your-collection-name"; + + deleteCollection(projectId, collectionName); + } +} +// [END firestore_data_delete_collection] diff --git a/firebase/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java b/firebase/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java new file mode 100644 index 00000000000..a1bfbf7540f --- /dev/null +++ b/firebase/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java @@ -0,0 +1,68 @@ +/* + * 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.firebase; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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 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); + + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @AfterAll + public static void tearDown() { + System.setOut(null); + } + + @Test + public void testDeleteCollection() throws ExecutionException, InterruptedException { + DeleteCollection.deleteCollection(PROJECT_ID, COLLECTION_NAME); + + String output = bout.toString(); + assertTrue(output.contains("Collection and all its subcollections deleted successfully.")); + } +} diff --git a/firebase/samples/src/test/java/com/example/firestore/Utils.java b/firebase/samples/src/test/java/com/example/firestore/Utils.java new file mode 100644 index 00000000000..1defb292abb --- /dev/null +++ b/firebase/samples/src/test/java/com/example/firestore/Utils.java @@ -0,0 +1,38 @@ +/* + * 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.firebase; + +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(); + Firestore db = firestoreOptions.getService(); + Map docData = new HashMap<>(); + docData.put("name", name); + ApiFuture future = db.collection(collectionName).document(documentName).set(docData); + future.get(); + } +} diff --git a/firestore/samples/pom.xml b/firestore/samples/pom.xml new file mode 100644 index 00000000000..d8db420ff4b --- /dev/null +++ b/firestore/samples/pom.xml @@ -0,0 +1,72 @@ + + + + 4.0.0 + com.example.firebase + firebase-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..13c98199af6 --- /dev/null +++ b/firestore/samples/src/main/java/com/example/firestore/DeleteCollection.java @@ -0,0 +1,50 @@ +/* + * 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.firebase; + +// [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; +import java.util.concurrent.ExecutionException; + +public class DeleteCollection { + + /** Delete a collection and all its subcollections. */ + public static void deleteCollection(String projectId, String collectionName) + throws ExecutionException, InterruptedException { + FirestoreOptions firestoreOptions = + FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build(); + 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 ExecutionException, InterruptedException { + String projectId = "your-project-id"; + String collectionName = "your-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..a1bfbf7540f --- /dev/null +++ b/firestore/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java @@ -0,0 +1,68 @@ +/* + * 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.firebase; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.util.concurrent.ExecutionException; +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 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); + + bout = new ByteArrayOutputStream(); + System.setOut(new PrintStream(bout)); + } + + @AfterAll + public static void tearDown() { + System.setOut(null); + } + + @Test + public void testDeleteCollection() throws ExecutionException, InterruptedException { + 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..1defb292abb --- /dev/null +++ b/firestore/samples/src/test/java/com/example/firestore/Utils.java @@ -0,0 +1,38 @@ +/* + * 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.firebase; + +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(); + Firestore db = firestoreOptions.getService(); + Map docData = new HashMap<>(); + docData.put("name", name); + ApiFuture future = db.collection(collectionName).document(documentName).set(docData); + future.get(); + } +} From 632ff3aa704e53ca4985d27e848070a31f644d54 Mon Sep 17 00:00:00 2001 From: FRANCISCO JAVIER ALARCON ESPARZA Date: Sat, 9 May 2026 00:19:45 +0000 Subject: [PATCH 3/4] update folder --- firebase/samples/pom.xml | 72 ------------------- .../example/firebase/DeleteCollection.java | 50 ------------- .../example/firestore/DeleteCollection.java | 50 ------------- .../example/firebase/DeleteCollectionIT.java | 68 ------------------ .../test/java/com/example/firebase/Utils.java | 38 ---------- .../example/firestore/DeleteCollectionIT.java | 68 ------------------ .../java/com/example/firestore/Utils.java | 38 ---------- firestore/samples/pom.xml | 4 +- .../example/firestore/DeleteCollection.java | 2 +- .../example/firestore/DeleteCollectionIT.java | 4 +- .../java/com/example/firestore/Utils.java | 2 +- 11 files changed, 7 insertions(+), 389 deletions(-) delete mode 100644 firebase/samples/pom.xml delete mode 100644 firebase/samples/src/main/java/com/example/firebase/DeleteCollection.java delete mode 100644 firebase/samples/src/main/java/com/example/firestore/DeleteCollection.java delete mode 100644 firebase/samples/src/test/java/com/example/firebase/DeleteCollectionIT.java delete mode 100644 firebase/samples/src/test/java/com/example/firebase/Utils.java delete mode 100644 firebase/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java delete mode 100644 firebase/samples/src/test/java/com/example/firestore/Utils.java diff --git a/firebase/samples/pom.xml b/firebase/samples/pom.xml deleted file mode 100644 index d8db420ff4b..00000000000 --- a/firebase/samples/pom.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - - 4.0.0 - com.example.firebase - firebase-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/firebase/samples/src/main/java/com/example/firebase/DeleteCollection.java b/firebase/samples/src/main/java/com/example/firebase/DeleteCollection.java deleted file mode 100644 index 13c98199af6..00000000000 --- a/firebase/samples/src/main/java/com/example/firebase/DeleteCollection.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.firebase; - -// [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; -import java.util.concurrent.ExecutionException; - -public class DeleteCollection { - - /** Delete a collection and all its subcollections. */ - public static void deleteCollection(String projectId, String collectionName) - throws ExecutionException, InterruptedException { - FirestoreOptions firestoreOptions = - FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build(); - 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 ExecutionException, InterruptedException { - String projectId = "your-project-id"; - String collectionName = "your-collection-name"; - - deleteCollection(projectId, collectionName); - } -} -// [END firestore_data_delete_collection] diff --git a/firebase/samples/src/main/java/com/example/firestore/DeleteCollection.java b/firebase/samples/src/main/java/com/example/firestore/DeleteCollection.java deleted file mode 100644 index 13c98199af6..00000000000 --- a/firebase/samples/src/main/java/com/example/firestore/DeleteCollection.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.firebase; - -// [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; -import java.util.concurrent.ExecutionException; - -public class DeleteCollection { - - /** Delete a collection and all its subcollections. */ - public static void deleteCollection(String projectId, String collectionName) - throws ExecutionException, InterruptedException { - FirestoreOptions firestoreOptions = - FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build(); - 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 ExecutionException, InterruptedException { - String projectId = "your-project-id"; - String collectionName = "your-collection-name"; - - deleteCollection(projectId, collectionName); - } -} -// [END firestore_data_delete_collection] diff --git a/firebase/samples/src/test/java/com/example/firebase/DeleteCollectionIT.java b/firebase/samples/src/test/java/com/example/firebase/DeleteCollectionIT.java deleted file mode 100644 index a1bfbf7540f..00000000000 --- a/firebase/samples/src/test/java/com/example/firebase/DeleteCollectionIT.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.firebase; - -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.concurrent.ExecutionException; -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 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); - - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - @AfterAll - public static void tearDown() { - System.setOut(null); - } - - @Test - public void testDeleteCollection() throws ExecutionException, InterruptedException { - DeleteCollection.deleteCollection(PROJECT_ID, COLLECTION_NAME); - - String output = bout.toString(); - assertTrue(output.contains("Collection and all its subcollections deleted successfully.")); - } -} diff --git a/firebase/samples/src/test/java/com/example/firebase/Utils.java b/firebase/samples/src/test/java/com/example/firebase/Utils.java deleted file mode 100644 index 1defb292abb..00000000000 --- a/firebase/samples/src/test/java/com/example/firebase/Utils.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.firebase; - -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(); - Firestore db = firestoreOptions.getService(); - Map docData = new HashMap<>(); - docData.put("name", name); - ApiFuture future = db.collection(collectionName).document(documentName).set(docData); - future.get(); - } -} diff --git a/firebase/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java b/firebase/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java deleted file mode 100644 index a1bfbf7540f..00000000000 --- a/firebase/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.firebase; - -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.concurrent.ExecutionException; -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 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); - - bout = new ByteArrayOutputStream(); - System.setOut(new PrintStream(bout)); - } - - @AfterAll - public static void tearDown() { - System.setOut(null); - } - - @Test - public void testDeleteCollection() throws ExecutionException, InterruptedException { - DeleteCollection.deleteCollection(PROJECT_ID, COLLECTION_NAME); - - String output = bout.toString(); - assertTrue(output.contains("Collection and all its subcollections deleted successfully.")); - } -} diff --git a/firebase/samples/src/test/java/com/example/firestore/Utils.java b/firebase/samples/src/test/java/com/example/firestore/Utils.java deleted file mode 100644 index 1defb292abb..00000000000 --- a/firebase/samples/src/test/java/com/example/firestore/Utils.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.firebase; - -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(); - Firestore db = firestoreOptions.getService(); - Map docData = new HashMap<>(); - docData.put("name", name); - ApiFuture future = db.collection(collectionName).document(documentName).set(docData); - future.get(); - } -} diff --git a/firestore/samples/pom.xml b/firestore/samples/pom.xml index d8db420ff4b..978b9a5e01b 100644 --- a/firestore/samples/pom.xml +++ b/firestore/samples/pom.xml @@ -18,8 +18,8 @@ xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.example.firebase - firebase-samples + com.example.firestore + firestore-samples 1.0-SNAPSHOT