-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(firestore): add firestore recursive delete sample #10263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alarconesparza
wants to merge
4
commits into
GoogleCloudPlatform:main
Choose a base branch
from
alarconesparza:alarconesparza-firebase-delete-collection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!-- | ||
| 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. | ||
| --> | ||
| <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 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"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.example.firestore</groupId> | ||
| <artifactId>firestore-samples</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <!-- | ||
| The parent pom defines common style checks and testing strategies for our samples. | ||
| Removing or replacing it should not affect the execution of the samples in anyway. | ||
| --> | ||
| <parent> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <groupId>com.google.cloud.samples</groupId> | ||
| <version>1.2.2</version> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>21</maven.compiler.source> | ||
| <maven.compiler.target>21</maven.compiler.target> | ||
| </properties> | ||
|
|
||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <artifactId>libraries-bom</artifactId> | ||
| <groupId>com.google.cloud</groupId> | ||
| <scope>import</scope> | ||
| <type>pom</type> | ||
| <version>26.80.0</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-firestore</artifactId> | ||
| </dependency> | ||
|
|
||
| <!-- Test dependencies --> | ||
| <dependency> | ||
| <artifactId>truth</artifactId> | ||
| <groupId>com.google.truth</groupId> | ||
| <scope>test</scope> | ||
| <version>1.4.5</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.junit.jupiter</groupId> | ||
| <artifactId>junit-jupiter</artifactId> | ||
| <version>5.14.3</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
54 changes: 54 additions & 0 deletions
54
firestore/samples/src/main/java/com/example/firestore/DeleteCollection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<Void> 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] | ||
69 changes: 69 additions & 0 deletions
69
firestore/samples/src/test/java/com/example/firestore/DeleteCollectionIT.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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.")); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
firestore/samples/src/test/java/com/example/firestore/Utils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Object> docData = new HashMap<>(); | ||
| docData.put("name", name); | ||
| ApiFuture<WriteResult> future = | ||
| db.collection(collectionName).document(documentName).set(docData); | ||
| future.get(); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.