Skip to content

samples(storagecontrol): add delete folder recursive sample#2216

Closed
nidhiii-27 wants to merge 1 commit into
mainfrom
add-delete-folder-recursive-sample
Closed

samples(storagecontrol): add delete folder recursive sample#2216
nidhiii-27 wants to merge 1 commit into
mainfrom
add-delete-folder-recursive-sample

Conversation

@nidhiii-27

Copy link
Copy Markdown

This PR adds the SDK sample and tests for the hierarchical namespace recursive delete feature, resolving b/521168740.

@nidhiii-27 nidhiii-27 requested review from a team as code owners June 10, 2026 09:12
@snippet-bot

snippet-bot Bot commented Jun 10, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label Bot added the samples Issues that are directly related to samples. label Jun 10, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new PHP sample delete_folder_recursive.php for recursively deleting folders in Google Cloud Storage using the Storage Control API, along with its corresponding integration test DeleteFolderRecursiveTest.php. Feedback was provided to improve the robustness of the test's teardown method (tearDownAfterClass) by adding a null check for the bucket and explicitly attempting to clean up the created folder to prevent resource leaks and fatal errors during failures.

Comment on lines +63 to +69
public static function tearDownAfterClass(): void
{
foreach (self::$sourceBucket->objects(['versions' => true]) as $object) {
$object->delete();
}
self::$sourceBucket->delete();
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

If setUpBeforeClass fails (e.g., due to bucket creation issues), self::$sourceBucket will remain uninitialized or null. When tearDownAfterClass runs, calling self::$sourceBucket->objects() will trigger a fatal error (Call to a member function objects() on null), which masks the original setup exception and makes debugging difficult.

Additionally, folders in Hierarchical Namespace buckets are distinct resources and are not returned by standard object listing. If the test fails after creating the folder but before the sample deletes it, the folder will persist, causing self::$sourceBucket->delete() to fail with a 409 Conflict and leaking the bucket.

Adding a null check for self::$sourceBucket and attempting to delete the folder in tearDownAfterClass ensures robust cleanup and prevents resource leaks.

    public static function tearDownAfterClass(): void
    {
        if (self::$sourceBucket) {
            if (self::$storageControlClient && self::$folderName) {
                try {
                    self::$storageControlClient->deleteFolder(self::$folderName);
                } catch (\Throwable $e) {
                    // Folder might already be deleted or not created
                }
            }
            foreach (self::$sourceBucket->objects(['versions' => true]) as $object) {
                $object->delete();
            }
            self::$sourceBucket->delete();
        }
    }

@nidhiii-27 nidhiii-27 closed this Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant