1

I have a image as a column in a class. I need to delete that image for a particular row by querying that row. But after querying how to delete that image

I can get the image url by :

var image = results[i].get("imageFile").url();

Can we delete the image by getting the url or is there any other way?

Technoid
  • 435
  • 3
  • 11

3 Answers3

6

Deleting files is only supported via the REST API at this time. You can try using Parse.Cloud.httpRequest to issue the delete file command.

You can try to do something like this :

var image = result.get("imageFile").url();    
Parse.Cloud.httpRequest({
        method: 'DELETE',
        url: image.substring(image.lastIndexOf("/")+1),
        headers: {
            "X-Parse-Application-Id": "YOUR_APP_ID
            "X-Parse-REST-API-Key" : YOUR_API_KEY"
        }
    );

But do not forget to get yourURL = just the name of your file.

That's why you need to do

image.substring(image.lastIndexOf("/")+1),

Exemple yourURL should NOT be http://files.parsetfss.com/19728287-9868-4728-8e49-31472daf0211/tfss-65ff02e3-3d11-45a8-ba25-4955f6c7f677-143569529.jpg

but tfss-65ff02e3-3d10-45a8-ba25-4955f6c7f677-1435695290.jpg

Toucouleur
  • 1,194
  • 1
  • 10
  • 30
  • yeah thanks... I did it by using name "var image = obj.get('imagefile').name();" and then "var url = "https://api.parse.com/1/files/ + image"; – Technoid Jul 01 '15 at 08:14
  • this deletion is really a mess, I have asked already several times why they didn't want to implement any specific function in SDK rather than requesting the REST API – Toucouleur Jul 01 '15 at 09:46
  • 1
    It did not work with "X-Parse-REST-API-Key" for me, used "X-Parse-Master-Key" instead. – erkanyildiz Jul 01 '15 at 11:10
  • Yes... And do you know how to check if file is empty.. For example in the above case how to check whether "image" is empty or not. Empty means that it has an address associated with it, but there is no file at that location. – Technoid Jul 01 '15 at 11:10
  • not sure I understood your question but `if (result.get("imageFile") !== undefined)` should do the trick `if (result.get("imageFile"))` as well – Toucouleur Jul 02 '15 at 12:45
  • Where does "result" come from? Or "obj" like you later used. Where are they defined and how? With what parameters? – Benni Dec 29 '15 at 20:58
  • I've added the function to Parse Cloud but I'm getting getaddrinfo ENOTFOUND https (Code: 141, Version: 1.19.1) error in the console. I've tried passing the full path to the file as well as just the name of the file but both failed. What am I missing? I don't quite understand the error. – C0D3 May 10 '21 at 07:06
1

Years later I happened upon this question as this SO page is linked from a closed issue on ParseServer->S3-Adapter github repo.

The information above, and in the docs, is slightly confusing. What is written in the Docs is that the URL will need to be:

https://YOUR.PARSE-SERVER.HERE/parse/files/...profile.png

This is correct, but only in the sense that you need to provide the path to your Parse Server, the "files" directory, and the file name itself. If you have multiple apps running from the same Parse Server (as is my case) this gets bit tricky.

What is suggested above - and is marked correct - is wrong or is no longer accurate (four years later) given that this isn't being run on Parse's own servers any longer.

Passing the file name by itself will not work. You need to pass in a valid URL - e.g.

http://localhost:8000/APP-NAME/files/FILE-NAME

Unlike in the documentation you don't need /parse/. What replaces /parse/ is, instead, the name of your application.

What worked perfectly for me was:

// note: endpoint is defined outside of this function
// the endpoint is the app/server url - e.g.
// http://localhost:8000/some_parse_app_name

Parse.Cloud.define('deleteFile', (request) => {

  let encodedFileName = encodeURI(request.params.fileName)

  Parse.Cloud.httpRequest({
    headers: {
      'X-Parse-Application-Id': appId,
      'X-Parse-Master-Key': masterKey
    },
    method: 'DELETE',
    url: `${endpoint}/files/${encodedFileName}`,
  }).then(function (httpResponse) {
    // success
    console.log('Request succeeded: ' + httpResponse.text)
  }, function (httpResponse) {
    // error
    console.error('Request failed with response code ' + httpResponse.status)
  })
})

  • I'm trying to implement something similar but I see this in the console logs of the server: Warning: Duplicate cloud functions exist for deleteFile. Only the last one will be used and the others will be ignored... Also when I try to call the cloud function, I get the error: [Error]: getaddrinfo ENOTFOUND https (Code: 141, Version: 1.19.1). Is it not able to find the file for some reason? Can't figure out what's happening – C0D3 May 10 '21 at 06:59
0

removing file on parse available two options. 1. Cloud Code 2. Rest API

for alternative you can make remove file without cloud code. just using javascript XMLHttpRequest for call api

    const remove = async function(URL) {
      const httpRequest = await fetch(URL, {
        method: 'DELETE',
        headers: {
          'X-Parse-Application-Id': appId,
          'X-Parse-REST-API-Key': resKey,
          'X-Parse-Master-Key': masterKey
        }
      });
      const response = await httpRequest;
      return response
    }