Skip to content
Open
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
96 changes: 96 additions & 0 deletions storage-control/deleteFolderRecursive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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.

'use strict';

function main(bucketName, folderName) {
// [START storage_control_delete_folder_recursive]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/

// The name of your GCS bucket
// const bucketName = 'bucketName';

// The name of the folder to be deleted recursively
// const folderName = 'folderName';

// Imports the Control library
const {StorageControlClient} = require('@google-cloud/storage-control').v2;

// Instantiates a client
const controlClient = new StorageControlClient();

if (
controlClient.auth &&
typeof controlClient.auth.getClient === 'function'
) {
const originalGetClient = controlClient.auth.getClient.bind(
controlClient.auth
);
controlClient.auth.getClient = async (...args) => {
const client = await originalGetClient(...args);
if (client) {
client.quotaProjectId = undefined;
}
return client;
};
}

async function callDeleteFolderRecursive() {
const folderPath = controlClient.folderPath('_', bucketName, folderName);

// Create the request
const request = {
name: folderPath,
};

// Run request
console.log(`Deleting folder recursively: ${folderName}`);
const [operation] = await controlClient.deleteFolderRecursive(request);

let op = operation.latestResponse;
while (!op.done) {
await new Promise(resolve => setTimeout(resolve, 1000));
const [latestOp] = await controlClient.operationsClient.getOperation(
{name: op.name},
{
otherArgs: {
headers: {
'x-goog-request-params': `name=${encodeURIComponent(op.name)}`,
},
},
}
);
op = latestOp;
}

if (op.error) {
throw new Error(
`Failed to delete folder recursively: ${op.error.message}`
);
}

console.log(`Deleted folder: ${folderName}.`);

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.

high

If the recursive delete operation fails, op.done will be true but op.error will be populated. Currently, the code will incorrectly log that the folder was successfully deleted even if the operation failed. We should check for op.error and throw an error if it exists.

    if (op.error) {
      throw new Error(op.error.message);
    }
    console.log("Deleted folder: " + folderName + ".");

}

callDeleteFolderRecursive();
// [END storage_control_delete_folder_recursive]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
2 changes: 1 addition & 1 deletion storage-control/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@google-cloud/storage": "^7.17.0",
"@google-cloud/storage-control": "^0.5.0",
"@google-cloud/storage-control": "^0.10.0",
"c8": "^10.0.0",
"chai": "^4.5.0",
"mocha": "^10.7.0",
Expand Down
16 changes: 16 additions & 0 deletions storage-control/system-test/folders.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,20 @@ describe('Folders', () => {
assert.match(output, /Deleted folder:/);
assert.match(output, new RegExp(folderName));
});

it('should delete a folder recursively', async () => {
const parentFolder = uuid.v4();
const childFolder = `${parentFolder}/${uuid.v4()}`;

// Create parent folder
execSync(`node createFolder.js ${bucketName} ${parentFolder}`);
// Create child folder
execSync(`node createFolder.js ${bucketName} ${childFolder}`);

const output = execSync(
`node deleteFolderRecursive.js ${bucketName} ${parentFolder}`
);
assert.match(output, /Deleted folder:/);
assert.match(output, new RegExp(parentFolder));
});
});
Loading