Skip to content

Commit 9a23e8f

Browse files
committed
samples(storagecontrol): add delete folder recursive sample
[Generated-by: AI]
1 parent 7b33bfc commit 9a23e8f

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.storage.control.v2;
18+
19+
// [START storage_control_delete_folder_recursive]
20+
import com.google.storage.control.v2.DeleteFolderRecursiveRequest;
21+
import com.google.storage.control.v2.FolderName;
22+
import com.google.storage.control.v2.StorageControlClient;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
public final class DeleteFolderRecursive {
27+
28+
public static void deleteFolderRecursive(String bucketName, String folderName)
29+
throws IOException, ExecutionException, InterruptedException {
30+
// The name of the bucket
31+
// String bucketName = "your-unique-bucket-name";
32+
33+
// The name of the folder within the bucket
34+
// String folderName = "your-unique-folder-name";
35+
36+
try (StorageControlClient storageControl = StorageControlClient.create()) {
37+
38+
// Set project to "_" to signify globally scoped bucket
39+
String formattedName = FolderName.format("_", bucketName, folderName);
40+
41+
DeleteFolderRecursiveRequest request =
42+
DeleteFolderRecursiveRequest.newBuilder()
43+
.setName(formattedName)
44+
.build();
45+
46+
storageControl.deleteFolderRecursiveAsync(request).get();
47+
48+
System.out.printf("Deleted folder: %s%n", formattedName);
49+
}
50+
}
51+
}
52+
// [END storage_control_delete_folder_recursive]

java-storage/samples/snippets/src/test/java/com/example/storage/control/v2/FoldersTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,25 @@ public void listFolder() throws IOException {
155155
storageControl.deleteFolder(folderName);
156156
}
157157
}
158+
@Test
159+
public void deleteFolderRecursive() throws IOException, ExecutionException, InterruptedException {
160+
FolderName folderName = FolderName.of("_", bucket.getName(), UUID.randomUUID().toString());
161+
Folder gen1 =
162+
storageControl.createFolder(
163+
BucketName.of("_", bucket.getName()),
164+
Folder.getDefaultInstance(),
165+
folderName.getFolder());
166+
167+
FolderName subFolderName = FolderName.of("_", bucket.getName(), folderName.getFolder() + "/subfolder");
168+
Folder gen2 =
169+
storageControl.createFolder(
170+
BucketName.of("_", bucket.getName()),
171+
Folder.getDefaultInstance(),
172+
subFolderName.getFolder());
173+
174+
DeleteFolderRecursive.deleteFolderRecursive(bucket.getName(), folderName.getFolder());
175+
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(folderName.toString());
176+
assertThrows(NotFoundException.class, () -> storageControl.getFolder(folderName));
177+
assertThrows(NotFoundException.class, () -> storageControl.getFolder(subFolderName));
178+
}
158179
}

0 commit comments

Comments
 (0)