Node.js AWS S3: Delete File from an S3 Bucket

Back to Index

Demonstrates how to delete a file from an S3 bucket. This example deletes the file /chilkat.ocean/starfishCopy.jpg. A response status code equal to 204 is returned for success (with an empty response body).

Documentation: https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectDELETE.html

CURL Command

curl -X DELETE https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg

Node.js Example

var os = require('os');
if (os.platform() == 'win32') {  
    var chilkat = require('chilkat_node10_win32'); 
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('chilkat_node10_arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('chilkat_node10_linux32');
    } else {
        var chilkat = require('chilkat_node10_linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('chilkat_node10_macosx');
}

function chilkatExample() {

    var rest = new chilkat.Rest();
    var success;

    var authAws = new chilkat.AuthAws();
    authAws.AccessKey = "AWS_ACCESS_KEY";
    authAws.SecretKey = "AWS_SECRET_KEY";
    authAws.Region = "us-west-2";
    authAws.ServiceName = "s3";
    rest.SetAuthAws(authAws);

    //  URL: https://chilkat.ocean.s3.us-west-2.amazonaws.com/starfishCopy.jpg
    var bTls = true;
    var port = 443;
    var bAutoReconnect = true;
    success = rest.Connect("chilkat.ocean.s3.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);
    if (success !== true) {
        console.log("ConnectFailReason: " + rest.ConnectFailReason);
        console.log(rest.LastErrorText);
        return;
    }

    var sbResponseBody = new chilkat.StringBuilder();
    success = rest.FullRequestNoBodySb("DELETE","/starfishCopy.jpg",sbResponseBody);
    if (success !== true) {
        console.log(rest.LastErrorText);
        return;
    }

    var respStatusCode = rest.ResponseStatusCode;
    if (respStatusCode >= 400) {
        console.log("Response Status Code = " + respStatusCode);
        console.log("Response Header:");
        console.log(rest.ResponseHeader);
        console.log("Response Body:");
        console.log(sbResponseBody.GetAsString());
        return;
    }


}

chilkatExample();