• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Node.js SDK)
  4. Object Management
  5. Deleting Objects

Deleting Objects

Deleting a Single Object

You can call ObsClient.deleteObject to delete a single object. Sample code is as follows:

// Import the OBS library.
var ObsClient = require('./lib/obs');

// Create an instance of ObsClient.
var obsClient = new ObsClient({
    access_key_id: '*** Provide your Access Key ***',       
    secret_access_key: '*** Provide your Secret Key ***',       
    server : 'yourdomainname'
});

obsClient.deleteObject({
       Bucket: 'bucketname',
       Key : 'objectkey'
}, (err, result) => {
       if(err){
              console.log('Error-->' + err);
       }else{
              console.log('Status-->' + result.CommonMsg.Status);
       }
});

Batch Deleting Objects

You can call ObsClient.deleteObjects to delete objects in a batch.

A maximum of 1000 objects can be deleted each time. Two response modes are supported: verbose (detailed) and quiet (brief).

  • In verbose mode (default mode), the returned response includes the deletion result of each requested object.
  • In quiet mode, the returned response includes only results of objects failed to be deleted.

Sample code:

// Import the OBS library.
var ObsClient = require('./lib/obs'); 
 
// Create an instance of ObsClient.
var obsClient = new ObsClient({ 
    access_key_id: '*** Provide your Access Key ***',        
    secret_access_key: '*** Provide your Secret Key ***',        
    server : 'yourdomainname' 
}); 
 
obsClient.deleteObjects({ 
       Bucket: 'bucketname', 
// Set the response mode to verbose.
       Quiet : false, 
       Objects : [{Key:'objectkey1', VersionId : null},{Key:'objectkey2', VersionId : null}, {Key : 'objectkey3', VersionId : null}] 
}, (err, result) => { 
       if(err){ 
              console.log('Error-->' + err); 
       }else{ 
              console.log('Status-->' + result.CommonMsg.Status); 
              if(result.CommonMsg.Status < 300 && result.InterfaceResult){
// Obtain the list of successfully deleted objects.
                console.log('Deleteds:'); 
                for(let i=0;i<result.InterfaceResult.Deleteds.length;i++){ 
                       console.log('Deleted[' + i + ']:'); 
                       console.log('Key-->'+result.InterfaceResult.Deleteds[i]['Key']); 
                       console.log('VersionId-->' + result.InterfaceResult.Deleteds[i]['VersionId']); 
                } 
// Obtain the list of objects failed to be deleted.
                console.log('Errors:'); 
                for(let i=0;i<result.InterfaceResult.Errors.length;i++){ 
                       console.log('Error[' + i + ']:'); 
                       console.log('Key-->' + result.InterfaceResult.Errors[i]['Key']); 
                       console.log('VersionId-->' + result.InterfaceResult.Errors[i]['VersionId']); 
                } 
              }
       } 
});
NOTE:

Use the Quiet parameter to specify the response mode and the Objects parameter to specify the to-be-deleted objects.