How do I delete an object on AWS S3 using Javascript?

user3335960 picture user3335960 · Jan 3, 2015 · Viewed 47.1k times · Source

I want to delete a file from Amazon S3 using Javascript. I have already uploaded the file using Javascript. Any ideas?

Answer

jlalcazar picture jlalcazar · Jan 16, 2015

You can use the JS method from S3:

var AWS = require('aws-sdk');

AWS.config.loadFromPath('./credentials-ehl.json');

var s3 = new AWS.S3();
var params = {  Bucket: 'your bucket', Key: 'your object' };

s3.deleteObject(params, function(err, data) {
  if (err) console.log(err, err.stack);  // error
  else     console.log();                 // deleted
});

Be aware that S3 never returns it the object has been deleted. You have to check it before or after with getobject, headobject, waitfor, etc