Why I am not able to delete video from Cloudinary using node.js?

59 Views Asked by At

I want to delete an asset of type video from the Cloudinary storage by using node.js framework. This code is working fine to delete an asset of type image but not for type video.

I tried using the destroy function with public id of the asset.

const deleteFilesFromCloud = async function (filePublicId) {
    try {
        const response = await cloudinary.uploader
            .destroy(filePublicId);
        return response;
    } catch (error) {
        console.log('Error in deleting file from cloud');
        return null;
    }
}
1

There are 1 best solutions below

0
Aleksandar On

In Cloudinary, an asset is identified by not only the public_id, but only when that public_id is in combination with the resource_type (e.g, 'image', 'video', 'raw' - default is 'image') and type ('upload', 'private', 'authenticated' etc - default is 'upload'). You can have an image and a video both with the same exact public_id but they will be considered different assets because the resource_type and/or type is different.

In your case, you're only specifying a public_id and that means the default resource_type: "image" and type: "upload" will be used. Unless you have an image of 'type' upload in your cloud with that public_id, your destroy() call won't work.

In general, you will want to store in your database at least the public_id, resource_type and type (all returned as part of the Upload API response following a successful upload) as those three pieces of data will allow you to refer to any asset from your Cloudinary account via the API.

To resolve your current issue, you will want to add resource_type: "video" to your destroy() call so that Cloudinary can find the asset you're trying to refer to. (assuming your video was uploaded with 'type' upload - but if it's any other 'type' you will want to specify it explicitly in your method call as well):

const response = await cloudinary.uploader.destroy(filePublicId, { resource_type: "video" });

References: