Destroy method not working

JuDa_1505
JuDa_1505 Member Posts: 5

Hello. I'm working with MEVN stack and I have a controller for modifying users. Users have hojaDeVida (which refers to the resume/CV). If a new resume/CV is provided, the previous one should be deleted from cloudinary but for some reason, the 'destroy' method of the uploader object isn't working. The resume/CV is uploaded correctly and the public_id is correct though. The full controller code is attached.

Tagged:

Answers

  • Wissam
    Wissam Member, Cloudinary Staff Posts: 103

    Hi @JuDa_1505 ,

    Can you please share what is the response you are getting while using the destroy call?

    From the code, I see that the upload was done with resource_type=raw , so the destroy should also contain this parameter as well:
    await cloudinary.uploader.destroy(public_id, { resource_type: "raw" });

    Another issue that could be the case here is that if your asset is organized in folders, include the folder path when passing the public_id to the destroy method. For instance, if your asset is in the folder cuteAnimals, the public_id should be cuteAnimals/cuteKitten.

    Best Regards,

    Wissam

  • JuDa_1505
    JuDa_1505 Member Posts: 5
    edited July 28

    Ok so, I created a constant for the destroy so I can print its result and I got a "not found". Keep in mind that I'm not working with folders

  • JuDa_1505
    JuDa_1505 Member Posts: 5
    edited July 28

  • JuDa_1505
    JuDa_1505 Member Posts: 5
    edited July 28

    I created a constant for the destroy so I can print its result:

    And this is what I got (keep in mind that I'm not working with folders) :

  • Wissam
    Wissam Member, Cloudinary Staff Posts: 103

    Hi @JuDa_1505 ,

    Thank you for sharing the details and screenshots. The "not found" result indicates that the public_id provided the destroy method does not correspond to an existing resource in your Cloudinary account.

    Please check the following:

    1. Ensure that the public_id you are trying to delete is accurate. Log the public_id before the destroy call to verify it's correct.
    2. I would check the resource details before the destroy call.

    Best Regards,

    Wissam

  • JuDa_1505
    JuDa_1505 Member Posts: 5
    edited July 29

    I printed the public_id, it's accurate and correct but I figured out something…

    1. I changed the resource_type to "auto"
    2. If the preexisting resource is raw and an image or another raw resource is provided, I'll get a "not found" result
    3. If the preexisting resource is an image and another image or a raw resource is provided, the destroy will work:

  • atdatu
    atdatu Member, Cloudinary Staff Posts: 18

    Hi @JuDa_1505 ,
    When deleting an asset, the resource_type parameter must be defined with these 3 possible values: raw, image, or video. The resource_type must exactly match when calling the destroy method. If the resource_type was to auto then it will default to image value. This scenario will create a mismatch if the asset is raw or video. In the event a call to destroy an asset yielded a mismatch, then a "not found" would be returned. Please check our reference doc on the destroy method's optional parameter:

    https://cloudinary.com/documentation/image_upload_api_reference#destroy_optional_parameters

  • atdatu
    atdatu Member, Cloudinary Staff Posts: 18

    For the above example that you have provided, did you have the public URL of that raw asset? I'm not sure if you have read my response earlier where I explained that when a non-matching resource_type and public_id combination happens, then a "not found" response will be returned.