Delete image from Amazon S3 Storage

15,989

Solution 1

You have to use the key in this way and not just the name of the file that is to be deleted then it will work.

var s3 = AWS.S3(awsCredentials);
s3.deleteObject({
  Bucket: MY_BUCKET,
  Key: 'some/subfolders/nameofthefile1.extension'
},function (err,data){})

Solution 2

when you send the key dynamically, use local storage to store the key and send it dynamically to delete image

var s3 = AWS.S3(awsCredentials); 
s3.deleteObject({  
 Bucket: MY_BUCKET, 
 Key: 'yourFileKey' 
},function (err,data){})
Share:
15,989
Vladimir Gabrielyan
Author by

Vladimir Gabrielyan

Updated on June 12, 2022

Comments

  • Vladimir Gabrielyan
    Vladimir Gabrielyan almost 2 years

    I am trying to delete an image from Amazon S3 Storage, but it doesn't delete.

    I am using following code:

    var s3 = AWS.S3(awsCredentials);
    s3.deleteObject({
      Bucket: MY_BUCKET,
      Key: myKey
    },function (err,data){})
    

    but images that I want to delete still persist.

    I even used s3.deleteObjects function,it returns deleted object names, but when I checked the storage, the images were still there.