Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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
//
// https://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.

using Google.Cloud.Storage.Control.V2;
using System;

namespace Google.Cloud.Storage.Control.V2.Snippets
{
public class DeleteFolderRecursiveSample
{
public void DeleteFolderRecursive(string bucketName, string folderName)
{
// [START storage_control_delete_folder_recursive]
StorageControlClient storageControlClient = StorageControlClient.Create();

// Format: projects/{project}/buckets/{bucket}/folders/{folder}
string folderResourceName = $"projects/_/buckets/{bucketName}/folders/{folderName}";

storageControlClient.DeleteFolderRecursive(folderResourceName).PollUntilCompleted();
Comment on lines +27 to +30

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

To align with the project's use of strongly-typed resource names (like BucketName), consider using FolderName instead of manually formatting the resource name string. This reduces the risk of formatting errors and makes the code cleaner.

            FolderName folderResourceName = FolderName.FromProjectBucketFolder("_", bucketName, folderName);

            storageControlClient.DeleteFolderRecursive(folderResourceName).PollUntilCompleted();


Console.WriteLine($"Deleted folder {folderName} recursively");
// [END storage_control_delete_folder_recursive]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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
//
// https://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.

using Google.Cloud.ClientTesting;
using Google.Cloud.Storage.V1;
using Google.Cloud.Storage.Control.V2;
using System;
using Xunit;

namespace Google.Cloud.Storage.Control.V2.Snippets
{
[SnippetOutputCollector]
public class DeleteFolderRecursiveSampleTest
{
[SkippableFact]
public void TestDeleteFolderRecursive()
{
string projectId = Environment.GetEnvironmentVariable("TEST_PROJECT");
Skip.If(string.IsNullOrEmpty(projectId), "No TEST_PROJECT environment variable set.");

// Setup: Create HN bucket, add folder.
var storageClient = StorageClient.Create();
string bucketName = IdGenerator.FromGuid(prefix: "test-hn-bucket-");
string folderName = "test-folder";

var bucket = new Google.Apis.Storage.v1.Data.Bucket
{
Name = bucketName,
HierarchicalNamespace = new Google.Apis.Storage.v1.Data.Bucket.HierarchicalNamespaceData { Enabled = true },
IamConfiguration = new Google.Apis.Storage.v1.Data.Bucket.IamConfigurationData
{
UniformBucketLevelAccess = new Google.Apis.Storage.v1.Data.Bucket.IamConfigurationData.UniformBucketLevelAccessData { Enabled = true }
}
};

storageClient.CreateBucket(projectId, bucket);

try
{
var storageControlClient = StorageControlClient.Create();

var folder = new Folder { Name = $"projects/_/buckets/{bucketName}/folders/{folderName}" };
storageControlClient.CreateFolder(new CreateFolderRequest
{
ParentAsBucketName = BucketName.FromProjectBucket("_", bucketName),
FolderId = folderName,
Folder = folder
});

// Execution: Call sample method.
var sample = new DeleteFolderRecursiveSample();
sample.DeleteFolderRecursive(bucketName, folderName);

// Verification: Assert successful deletion by trying to get the folder.
var ex = Assert.Throws<Grpc.Core.RpcException>(() =>
storageControlClient.GetFolder(new GetFolderRequest { Name = folder.Name }));
Assert.Equal(Grpc.Core.StatusCode.NotFound, ex.StatusCode);
Comment on lines +66 to +68

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

According to the repository's general rules, when asserting that an exception is thrown in a test, we should also assert on the exception message to ensure the correct exception is being propagated.

                var ex = Assert.Throws<Grpc.Core.RpcException>(() => 
                    storageControlClient.GetFolder(new GetFolderRequest { Name = folder.Name }));
                Assert.Equal(Grpc.Core.StatusCode.NotFound, ex.StatusCode);
                Assert.Contains("not found", ex.Message, StringComparison.OrdinalIgnoreCase);
References
  1. When asserting that an exception is thrown in a test, also assert on the exception message to ensure the correct exception is being propagated.

}
finally
{
// Cleanup: Standard Dispose.
storageClient.DeleteBucket(bucketName);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net8.0;net462</TargetFrameworks>
Expand All @@ -15,6 +15,7 @@
<PackageReference Include="Xunit.SkippableFact" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<ProjectReference Include="..\..\Google.Cloud.Storage.V1\Google.Cloud.Storage.V1\Google.Cloud.Storage.V1.csproj" />
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>
Loading