Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.storage.control.v2;

// [START storage_control_delete_folder_recursive]

import com.google.storage.control.v2.DeleteFolderRecursiveRequest;
import com.google.storage.control.v2.FolderName;
import com.google.storage.control.v2.StorageControlClient;
import java.util.concurrent.ExecutionException;

public final class DeleteFolderRecursive {

public static void deleteFolderRecursive(String bucketName, String folderName)
throws ExecutionException, InterruptedException, Exception {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The throws clause is redundant because Exception is a superclass of ExecutionException and InterruptedException. Declaring them together is unnecessary. We can simplify this by throwing only Exception.

Suggested change
public static void deleteFolderRecursive(String bucketName, String folderName)
throws ExecutionException, InterruptedException, Exception {
public static void deleteFolderRecursive(String bucketName, String folderName)
throws Exception {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// The name of the bucket
// String bucketName = "your-unique-bucket-name";

// The name of the folder within the bucket
// String folderName = "your-unique-folder-name";

try (StorageControlClient storageControl = StorageControlClient.create()) {

// Set project to "_" to signify globally scoped bucket
String folderResourceName = FolderName.format("_", bucketName, folderName);
DeleteFolderRecursiveRequest request =
DeleteFolderRecursiveRequest.newBuilder().setName(folderResourceName).build();

storageControl.deleteFolderRecursiveAsync(request).get();

System.out.printf("Deleted folder recursively: %s%n", folderResourceName);
}
}
}
// [END storage_control_delete_folder_recursive]
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ public void deleteFolder() throws IOException {
assertThrows(NotFoundException.class, () -> storageControl.getFolder(folderName));
}

@Test
public void deleteFolderRecursive() throws Exception {
FolderName folderName = FolderName.of("_", bucket.getName(), UUID.randomUUID().toString());
Folder gen1 =
storageControl.createFolder(
BucketName.of("_", bucket.getName()),
Folder.getDefaultInstance(),
folderName.getFolder());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The variable gen1 is declared but never used. You can invoke createFolder directly without assigning its return value to an unused variable.

    storageControl.createFolder(
        BucketName.of("_", bucket.getName()),
        Folder.getDefaultInstance(),
        folderName.getFolder());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


DeleteFolderRecursive.deleteFolderRecursive(bucket.getName(), folderName.getFolder());
assertThat(stdOut.getCapturedOutputAsUtf8String()).contains(folderName.toString());
assertThrows(NotFoundException.class, () -> storageControl.getFolder(folderName));
}

@Test
public void listFolder() throws IOException {
FolderName folderName = FolderName.of("_", bucket.getName(), UUID.randomUUID().toString());
Expand Down
Loading