Search code examples
parse-platformparse-cloud-codeback4appsparse-file

Parse Server - How to delete image file from the server using cloud code


How can I delete an image's file from the server using Parse Cloud Code. I am using back4app.com

After Deleting Image Row

I am getting the images urls, then calling a function to delete the image using its url

Parse.Cloud.afterDelete("Image", function(request) {

    // get urls
    var imageUrl = request.object.get("image").url();
    var thumbUrl = request.object.get("thumb").url();
    if(imageUrl!=null){
        //delete
        deleteFile(imageUrl);
    }
    if(thumbUrl!=null){
        //delete
        deleteFile(thumbUrl);
    }
});

Delete the image file from the server

function deleteFile(url){

        Parse.Cloud.httpRequest({
            url: url.substring(url.lastIndexOf("/")+1),
            method: 'DELETE',
            headers: {
                'X-Parse-Application-Id': 'xxx',
                'X-Parse-Master-Key':     'xxx'
            }
        }).then(function(httpResponse) {
            console.log(httpResponse.text);
        }, function(httpResponse) {
             console.error('Request failed with response code ' + httpResponse.status);
        });
}

Solution

  • for security reasons, not is posible to delete directly the image from Back4App, using DELETE from SDK or REST API. I believe that you can follow the guide below:

    https://help.back4app.com/hc/en-us/articles/360002327652-How-to-delete-files-completely-