Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete multiple objects fron google cloud storage using node.js

I need to delete multiple objects from google cloud storage. I have deleted one object at a time.

This is my code:

var gcloud = require('gcloud')({
  projectId: "sampleProject1"
});
var gcs = gcloud.storage();
var myBucket = gcs.bucket('sampleBucket1');
var file = myBucket.file('1.png');

file.delete(function (err, apiResponse) {
  if (err) {
    console.log(err);
  }
  else {
    console.log("Deleted successfully");
  }
});

But I need to delete multiple objects simultaneously. Is it possible or not?

like image 821
Abdul Manaf Avatar asked Apr 04 '16 13:04

Abdul Manaf


People also ask

How do I delete files from Google Cloud Storage?

Navigate to the objects, which may be located in a folder. Click the checkbox for each object you want to delete. You can also click the checkbox for folders, which will delete all objects contained in that folder. Click the Delete button.

How do I delete projects from Google cloud?

In the Google Cloud console, go to the Manage resources page. In the project list, select the project that you want to delete, and then click Delete. In the dialog, type the project ID, and then click Shut down to delete the project.


1 Answers

We do have bucket#deleteFiles that will handle throttling the requests for you. You can use the prefix option to target multiple images by a naming convention, like:

bucket.deleteFiles({ prefix: 'image-' }, callback);

If that doesn't work, we also have a guide that shows how you can do the throttling logic yourself. See "My requests are returning errors instructing me to retry the request": https://googlecloudplatform.github.io/gcloud-node/#/docs/v0.29.0/guides/troubleshooting

Edit to elaborate on how to do the throttling using async:

var async = require('async');
var PARALLEL_LIMIT = 10;

function deleteFile(file, callback) {
  file.delete(callback);
}

async.eachLimit(filesToDelete, PARALLEL_LIMIT, deleteFile, function(err) {
  if (!err) {
    // Files deleted!
  }
});
like image 50
Stephen Avatar answered Sep 29 '22 09:09

Stephen