Kevin
Kevin

Reputation: 311

JavaScript aws-sdk S3 deleteObject(s) succedes but doesn't actually delete anything

In the MEAN.js app I'm building I upload images to AWS S3. I am trying to use the AWS SDK to delete unwanted images from the site but after a successful ajax call the file remains on S3.

I have required the AWS SDK like so, it works both with and without the config variables (as it should):

var aws = require('aws-sdk');
aws.config.update({accessKeyId: process.env.AWS_ACCESS_KEY_ID, secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY});

For my route I have the following code:

router.post('/delete', auth, function(req,res, next){
if(req.body.key) {
    var s3 = new aws.S3();
    var params = {
        Bucket: 'bucket name',
        Key: req.body.key
    };
    s3.deleteObject(params, function (err, data) {
        if (err) {
            console.log(err, err.stack);
            return next(err);
        }
        console.log(data);
        res.end('done');

I get a 200 response and {} is logged to the console but the file is not deleted from storage. I've also tried using the deleteObjects method like so:

var params = {
        Bucket: 'bucket name',
        Delete: {
            Objects: [
                {
                    Key: req.body.key
                }
            ]
        }

    };
    s3.deleteObjects(params, function (err, data) {
        if (err) {
            console.log(err, err.stack);
            return next(err);
        }
        console.log(data);
        res.end('done');

When I use deleteObjects I get { Deleted: [ { Key: 'file name' } ], Errors: [] } as a response but the file is still on S3.

Am I doing something wrong? I thought I followed the documentation to a T.

Also, issue occurs wether or not versioning is enabled on the bucket. With versioning enabled my response is:

{ Deleted: 
[ { Key: 'file name',
   DeleteMarker: true,
   DeleteMarkerVersionId: 'long id' } ],
Errors: [] }

Upvotes: 12

Views: 12788

Answers (5)

Yanov
Yanov

Reputation: 673

1000+ items

const data = await listAllObjectsFromS3Bucket(bucketParams.Bucket);
console.log("Delete data.length: ------> " +  data.length);

let size = 900; //size array
let subarray = []; //subarray
for (let i = 0; i < Math.ceil(data.length/size); i++){
    subarray[i] = data.slice((i*size), (i*size) + size);
}

for (let i = 0; i < subarray.length; i++) {
    const params = {
              Bucket: bucketParams.Bucket, 
              Delete: {
              Objects: subarray[i], 
              Quiet: true
          }
    };
    await S3Client.deleteObjects(params).promise()
};

Upvotes: 0

publicapps
publicapps

Reputation: 190

I've noticed AWS indicates the key as deleted even though it does not exist. In my case I sent the file path as the object key, but the key was actually the file path minus the leading /.

Upvotes: 0

Victor Godwin
Victor Godwin

Reputation: 1

I really don't think Aws takes long that long to delete. I was having same issue and solved it by changing the file name value, I was initially using the url instead of using the filename i used in uploading the image.

Upvotes: 0

ali
ali

Reputation: 91

Try this one. You need to use promise() to ensure that the object is deleted before ending the execution. 6 hours waiting just for a simple object deletion is not normal, even with considering S3 99.999999999% durability.

var params = {
        Bucket : bucket,
        Key : video
};
try {
    await s3.deleteObject(params,function(err,data){
        if (err)    console.log(err,err.stack);
        else        console.log("Response:",data);
    }).promise();
} catch (e) {}

Upvotes: 8

Kevin
Kevin

Reputation: 311

Looks like first comment was right, it takes some time for files to be removed from AWS S3. In this case it was over an hour until it disappeared (could have been 6 hours, I stepped away for quite a bit).

Upvotes: 6

Related Questions