Why don't Parse.File have delete method?

If I want to delete file, I need to make a DELETE request to parse server. But this is extra work. Why cant we delete file like we delete parse objects.

Something like this would be great:
await parseFile.delete({useMasterKey:true});

This would be much easier. Can you tell me why this is not the way to go. What are the vulnerabilities?

You can delete files using:

await file.destroy({ useMasterKey: true });

SDK Reference

1 Like

Thats great I looked in JS docs but didnt see that. Thank you. Docs needs to be updated or maybe they intensionally didnt add because its node specific? Anyway thank you again.

It’s a good suggestion. There a number of documentation issues which are up for grabs, this subject being one of them. Naturally as Parse Server continues to evolve, updating documentation requires a lot of time, time that often is spent on building new features :slight_smile: .

I’ve submitted a PR that will update the docs to include information around deleting files via JS.

1 Like

Assuming that I have array of [Parse.File] in the database object… I can’t find any Parse.File.destroyAll(files, {useMasterKey: true}); as there is for Parse.Object in the documentations so I have to loop one by one. Am I correct?

Parse.Cloud.afterDelete("Msg", async ({object, log}) => {  
	//delete files referred by the message
    const files = object.get("fls");
    try {
        for (let i = 0; i < files.length; ++i) {
            const file = files[i];
            await file.destroy({ useMasterKey: true });
        }
        //Parse.File.destroyAll(files, {useMasterKey: true});
    } catch (e) {
        log.error(`Error removing file objects: ${error.code}: ${error.message} referred in message ${object.id}`);
    }
	return;
});

Thank you!

If you don’t want to delete files one by one and want them to be deleted simoultanously you can use Promise.all

const files = object.get("fls");
const promises = [];
for (const file of files) {
    promises.push(file.destroy({ useMasterKey: true }));
} 
await Promise.all(promises);

if you want an ugly solution

const files = object.get("fls");
const deleteResult = await new Promise((resolve, reject) => {
    const result = [];
    let completed = 0;
    for (let i = 0; i < files.length; i++) {
        const file = files[i];
        file.destroy({ useMasterKey: true }).then(res => {
            result[i] = true;
            completed++;
            if (completed === files.length) {
                resolve(result);
            }
        }).catch(err => {
            result[i] = false;
            completed++;
            if (completed === files.length) {
                resolve(result);
            }
        });
    }
});
1 Like

I will take the nicer solution if I may! Thanks for great help!

You can also use the array method .map in many cases.

Promise.allSettled will resolve all the promises, even if some are rejected.


const files = object.get("fls") || []
await Promise.allSettled(files.map(file => file.destroy({ useMasterKey: true })));