Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
        });
}
like image 958
Hatim Avatar asked Jun 21 '26 19:06

Hatim


1 Answers

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-

like image 50
nataliec Avatar answered Jun 23 '26 10:06

nataliec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!