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;
}
}
In Cloudinary, an asset is identified by not only the
public_id, but only when thatpublic_idis in combination with theresource_type(e.g, 'image', 'video', 'raw' - default is 'image') andtype('upload', 'private', 'authenticated' etc - default is 'upload'). You can have an image and a video both with the same exactpublic_idbut they will be considered different assets because theresource_typeand/ortypeis different.In your case, you're only specifying a
public_idand that means the defaultresource_type: "image"andtype: "upload"will be used. Unless you have animageof 'type'uploadin your cloud with thatpublic_id, yourdestroy()call won't work.In general, you will want to store in your database at least the
public_id,resource_typeandtype(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 yourdestroy()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):References: