Deleting a Parse File using cloud code

#1

Hey guys, I’m having issues trying to delete a Parse file.

This is my deleteFile function in my cloud code:

Parse.Cloud.define('deleteFile', function(req, res) {
	const { img } = req.params;
	Parse.Cloud.httpRequest({
		method: 'DELETE',
		url: img,
		headers: {
			'X-Parse-Application-Id': 'appID',
			'X-Parse-Master-Key': 'masterKey'
		}
	})
		.then(httpResponse => console.log(httpResponse.text))
		.catch(error => console.log(error));
});

When running it in my app I keep getting Unsupported protocol null any ideas on what I’m doing wrong?

Thanks.

#2

hi @benmarte it depends what is the content of img.

According to the documentation to success the delete, img should be ${serverURL}/files/${file.name()}

#3

Have you taken a look at this SO question

Working with files is really weird as you have to perform httpRequests within the cloud code. I chose to store files in an S3 location and later in Cloudinary in order to get around this - probably not helpful to tell you that, but I recall it being such a pain that I changed tack.

#4

Hey guys I thought I posted the link to the SO question I was using as the base for my example here is the link: https://stackoverflow.com/questions/31146468/delete-parse-file-through-cloud-code but clearly, that is not working or no longer works.

#5

did u confirm that the img value you assigned to url is the file name received when storing the file and not an object reference ?

see reference here:

#6

I read somewhere that the files are deleted when the reference to the file is deleted, but i’m not sure where.and how reliable is this info.
I assume that files are stored in mongodb as default , but what you are doing is to store them as regular files is S3 , right ?

#7

I am saving directly in mongodb not S3, and yes I ran this function on the img var before sending it to the cloud code:

img = img.substring(img.lastIndexOf('/') + 1);

And I verified the filename is the same on the DB as the img var I am sending to the cloud code.

I also tried deleting via curl in my terminal and I get this result:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot DELETE /parse/files/turn2/d35012f25c28e98b906be3411fa4de3f_Screen%20Shot%202019-02-26%20at%2010.54.09%20AM.png</pre>
</body>
</html>

What am I doing wrong?

Thanks.

#8

Hope this tutorial will help you: https://blog.sashido.io/how-to-properly-delete-a-file-without-leaving-an-orphan/

1 Like