Skip to content
Open
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
72 changes: 72 additions & 0 deletions firestore/samples/pom.xml
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>
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);

Comment thread
alarconesparza marked this conversation as resolved.
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]
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."));
}
}
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();
}
}
}