lib/bucketUtils.js
function emptyBucket(aws, bucketName, keyPrefix) {
  return listObjectsInBucket(aws, bucketName).then(resp => {
    const contents = resp.Contents;
    let testPrefix = false,
      prefixRegexp;
    if (!contents[0]) {
      return Promise.resolve();
    } else {

      if (keyPrefix) {
        testPrefix = true;
        prefixRegexp = new RegExp('^' + keyPrefix);
      }
      const objects = contents.map(function(content) {
        return {Key: content.Key};
      }).filter(content => !testPrefix || prefixRegexp.test(content.Key));

      const params = {
        Bucket: bucketName,
        Delete: { Objects: objects }
      };

      return aws.request('S3', 'deleteObjects', params);
    }
  });
}
function emptyBucket(aws, bucketName, keyPrefix) {
  return listObjectsInBucket(aws, bucketName).then(resp => {
    const contents = resp.Contents;
    let testPrefix = false,
      prefixRegexp;
    if (!contents[0]) {
      return Promise.resolve();
    } else {

      if (keyPrefix) {
        testPrefix = true;
        prefixRegexp = new RegExp('^' + keyPrefix);
      }
      const objects = contents.map(function(content) {
        return {Key: content.Key};
      }).filter(content => !testPrefix || prefixRegexp.test(content.Key));

      const params = {
        Bucket: bucketName,
        Delete: { Objects: objects }
      };

      return aws.request('S3', 'deleteObjects', params);
    }
  });
}
function emptyBucket ( aws , bucketName , keyPrefix ) { return listObjectsInBucket ( aws , bucketName ) . then ( resp => { const contents = resp . Contents ; let testPrefix = false , prefixRegexp ; if ( ! contents [ 0 ] ) { return Promise . resolve ( ) ; } else { if ( keyPrefix ) { testPrefix = true ; prefixRegexp = new RegExp ( '^' + keyPrefix ) ; } const objects = contents . map ( function ( content ) { return { Key : content . Key } ; } ) . filter ( content => ! testPrefix || prefixRegexp . test ( content . Key ) ) ; const params = { Bucket : bucketName , Delete : { Objects : objects } } ; return aws . request ( 'S3' , 'deleteObjects' , params ) ; } } ) ; }
Deletes all objects in an S3 bucket
@param {Object} aws - AWS class
@param {string} bucketName - Name of bucket to be deleted
Deletes all objects in an S3 bucket