Skip to content

Commit f98745d

Browse files
committed
feat(nodejs): add deleteFolderRecursive sample
This adds a sample demonstrating how to recursively delete a folder in a hierarchical namespace bucket. Fixes: b/521168740
1 parent e29c4b7 commit f98745d

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2026 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
function main(bucketName, folderName) {
18+
// [START storage_control_delete_folder_recursive]
19+
/**
20+
* TODO(developer): Uncomment these variables before running the sample.
21+
*/
22+
23+
// The name of your GCS bucket
24+
// const bucketName = 'bucketName';
25+
26+
// The name of the folder to be deleted
27+
// const folderName = 'folderName';
28+
29+
// Imports the Control library
30+
const {StorageControlClient} = require('@google-cloud/storage-control').v2;
31+
32+
// Instantiates a client
33+
const controlClient = new StorageControlClient();
34+
35+
async function callDeleteFolderRecursive() {
36+
// Set project to "_" to signify globally scoped bucket
37+
const folderPath = controlClient.folderPath('_', bucketName, folderName);
38+
39+
// Create the request
40+
const request = {
41+
name: folderPath,
42+
};
43+
44+
// Run request
45+
const [operation] = await controlClient.deleteFolderRecursive(request);
46+
await operation.promise();
47+
console.log(`Deleted folder: ${folderPath}`);
48+
}
49+
50+
callDeleteFolderRecursive();
51+
// [END storage_control_delete_folder_recursive]
52+
}
53+
54+
process.on('unhandledRejection', err => {
55+
console.error(err.message);
56+
process.exitCode = 1;
57+
});
58+
main(...process.argv.slice(2));

storage-control/system-test/folders.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,14 @@ describe('Folders', () => {
7676
assert.match(output, /Deleted folder:/);
7777
assert.match(output, new RegExp(folderName));
7878
});
79+
80+
it('should delete a folder recursively', async () => {
81+
const recursiveFolderName = uuid.v4();
82+
execSync(`node createFolder.js ${bucketName} ${recursiveFolderName}`);
83+
const output = execSync(
84+
`node deleteFolderRecursive.js ${bucketName} ${recursiveFolderName}`
85+
);
86+
assert.match(output, /Deleted folder:/);
87+
assert.match(output, new RegExp(recursiveFolderName));
88+
});
7989
});

0 commit comments

Comments
 (0)