samples(storagecontrol): add delete folder recursive sample#2216
samples(storagecontrol): add delete folder recursive sample#2216nidhiii-27 wants to merge 1 commit into
Conversation
[Generated-by: AI]
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
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.
| public static function tearDownAfterClass(): void | ||
| { | ||
| foreach (self::$sourceBucket->objects(['versions' => true]) as $object) { | ||
| $object->delete(); | ||
| } | ||
| self::$sourceBucket->delete(); | ||
| } |
There was a problem hiding this comment.
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();
}
}
This PR adds the SDK sample and tests for the hierarchical namespace recursive delete feature, resolving b/521168740.