Skip to content

Commit a630c7b

Browse files
feat(firestore): add firestore recursive delete sample (#10263)
* feat(firebase): add firebase recursive delete sample * rename folder * update folder * address comments
1 parent 51c722e commit a630c7b

4 files changed

Lines changed: 236 additions & 0 deletions

File tree

firestore/samples/pom.xml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2026 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xmlns="http://maven.apache.org/POM/4.0.0"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<groupId>com.example.firestore</groupId>
22+
<artifactId>firestore-samples</artifactId>
23+
<version>1.0-SNAPSHOT</version>
24+
25+
<!--
26+
The parent pom defines common style checks and testing strategies for our samples.
27+
Removing or replacing it should not affect the execution of the samples in anyway.
28+
-->
29+
<parent>
30+
<artifactId>shared-configuration</artifactId>
31+
<groupId>com.google.cloud.samples</groupId>
32+
<version>1.2.2</version>
33+
</parent>
34+
35+
<properties>
36+
<maven.compiler.source>21</maven.compiler.source>
37+
<maven.compiler.target>21</maven.compiler.target>
38+
</properties>
39+
40+
<dependencyManagement>
41+
<dependencies>
42+
<dependency>
43+
<artifactId>libraries-bom</artifactId>
44+
<groupId>com.google.cloud</groupId>
45+
<scope>import</scope>
46+
<type>pom</type>
47+
<version>26.80.0</version>
48+
</dependency>
49+
</dependencies>
50+
</dependencyManagement>
51+
52+
<dependencies>
53+
<dependency>
54+
<groupId>com.google.cloud</groupId>
55+
<artifactId>google-cloud-firestore</artifactId>
56+
</dependency>
57+
58+
<!-- Test dependencies -->
59+
<dependency>
60+
<artifactId>truth</artifactId>
61+
<groupId>com.google.truth</groupId>
62+
<scope>test</scope>
63+
<version>1.4.5</version>
64+
</dependency>
65+
<dependency>
66+
<groupId>org.junit.jupiter</groupId>
67+
<artifactId>junit-jupiter</artifactId>
68+
<version>5.14.3</version>
69+
<scope>test</scope>
70+
</dependency>
71+
</dependencies>
72+
</project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.firestore;
18+
19+
// [START firestore_data_delete_collection]
20+
21+
import com.google.api.core.ApiFuture;
22+
import com.google.cloud.firestore.CollectionReference;
23+
import com.google.cloud.firestore.Firestore;
24+
import com.google.cloud.firestore.FirestoreOptions;
25+
26+
public class DeleteCollection {
27+
28+
/**
29+
* Delete a collection and all its subcollections.
30+
*
31+
* @param projectId The Google Cloud project ID
32+
* @param collectionName The name of the collection to delete
33+
*/
34+
public static void deleteCollection(String projectId, String collectionName) throws Exception {
35+
FirestoreOptions firestoreOptions =
36+
FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build();
37+
try (Firestore db = firestoreOptions.getService()) {
38+
CollectionReference collection = db.collection(collectionName);
39+
40+
ApiFuture<Void> future = db.recursiveDelete(collection);
41+
42+
future.get();
43+
System.out.println("Collection and all its subcollections deleted successfully.");
44+
}
45+
}
46+
47+
public static void main(String[] args) throws Exception {
48+
String projectId = "example-project-id";
49+
String collectionName = "example-collection-name";
50+
51+
deleteCollection(projectId, collectionName);
52+
}
53+
}
54+
// [END firestore_data_delete_collection]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.firestore;
18+
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
import java.io.ByteArrayOutputStream;
23+
import java.io.PrintStream;
24+
import org.junit.jupiter.api.AfterAll;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Test;
27+
28+
public class DeleteCollectionIT {
29+
30+
private static ByteArrayOutputStream bout;
31+
private static PrintStream prevPrintStream;
32+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
33+
private static final String COLLECTION_NAME = "usa-cities";
34+
private static final String DOCUMENT_NAME = "LA";
35+
private static final String NAME = "Los Angeles";
36+
37+
private static String getEnvVar(String varName) {
38+
String value = System.getenv(varName);
39+
assertNotNull(
40+
String.format("Environment variable '%s' must be set to perform these tests.", varName),
41+
value);
42+
return value;
43+
}
44+
45+
@BeforeAll
46+
public static void setUp() throws Exception {
47+
getEnvVar("GOOGLE_CLOUD_PROJECT");
48+
49+
// Create collection
50+
Utils.createCollection(PROJECT_ID, COLLECTION_NAME, DOCUMENT_NAME, NAME);
51+
52+
prevPrintStream = System.out;
53+
bout = new ByteArrayOutputStream();
54+
System.setOut(new PrintStream(bout));
55+
}
56+
57+
@AfterAll
58+
public static void tearDown() {
59+
System.setOut(prevPrintStream);
60+
}
61+
62+
@Test
63+
public void testDeleteCollection() throws Exception {
64+
DeleteCollection.deleteCollection(PROJECT_ID, COLLECTION_NAME);
65+
66+
String output = bout.toString();
67+
assertTrue(output.contains("Collection and all its subcollections deleted successfully."));
68+
}
69+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.firestore;
18+
19+
import com.google.api.core.ApiFuture;
20+
import com.google.cloud.firestore.Firestore;
21+
import com.google.cloud.firestore.FirestoreOptions;
22+
import com.google.cloud.firestore.WriteResult;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
public class Utils {
27+
public static void createCollection(
28+
String projectId, String collectionName, String documentName, String name) throws Exception {
29+
// Create collection and document to be deleted
30+
FirestoreOptions firestoreOptions =
31+
FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(projectId).build();
32+
try (Firestore db = firestoreOptions.getService()) {
33+
34+
Map<String, Object> docData = new HashMap<>();
35+
docData.put("name", name);
36+
ApiFuture<WriteResult> future =
37+
db.collection(collectionName).document(documentName).set(docData);
38+
future.get();
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)