v2.uploader.destroy function not wokring

Sarmad
Sarmad Member Posts: 8
edited February 29 in Developer APIs

Hi, i am trying to delete a resource from cloudinary using publicId, but its not working. It does console .log 'Resource deleted successfully:', undefined;

It's in nest Js

Below is my code: 

private destroy(publicId: string): Promise<void> {

    console.log('this is public id', publicId);

    return new Promise((resolve, reject) => {

      v2.uploader.destroy(publicId, { invalidate: true }, (error, result) => {

        if (error) {

          console.error('Error deleting resource:', error);

          reject(error);

        } else {

          console.log('Resource deleted successfully:', result);

          resolve(result);

        }

      });

    });

  }

Tagged:

Best Answers

  • Tom
    Tom Member, Cloudinary Staff Posts: 109
    Answer ✓

    So it looks like you are not passing the full folder path for gkzdhra7qwkqyuovcojc which would result in a 404.

    In fixed folder mode, the folder path is part of the public_id so you need to make sure to include it.

    The public_id should be resources/gkzdhra7qwkqyuovcojc instead as it's in a folder called resources, can you please pass this and let me know if it works this time?

    Thanks,

    Thomas

  • Sarmad
    Sarmad Member Posts: 8
    Answer ✓

    Thanks yeah tried this way it after my previous comment, it worked.

    Providing below code in case someone stuck in this situation:

      private extractPublicId(imageUrl: string): string {

        const parts = imageUrl.split('/');

        const fileName = parts.pop();

        const publicId = fileName?.split('.')[0];

        const folder = parts.pop();

        return folder ? `${folder}/${publicId}` : publicId || '';

      }

Answers

  • Tom
    Tom Member, Cloudinary Staff Posts: 109

    Hi @Sarmad ,

    Thanks for reaching out.

    If you are getting successful response from the API then probably you are still seeing a cached copy of the asset in your browser.

    Can you do a hard-refresh or check in incognito mode or a different browser to rule this out?

    Kind Regards,

    Thomas

  • Sarmad
    Sarmad Member Posts: 8

    why is it then console logging this "Undefined" ?

    console .log 'Resource deleted successfully:', undefined;


     if (error) {


         console.error('Error deleting resource:', error);


         reject(error);


        } else {


         console.log('Resource deleted successfully:', result);


         resolve(result);


        }

  • Tom
    Tom Member, Cloudinary Staff Posts: 109

    Hi @Sarmad ,

    Thanks for replying.

    Can you please give us a full sample of the delete call you are making with the public_id?

    Kind Regards,

    Thomas

  • Sarmad
    Sarmad Member Posts: 8

     async deleteByUrl(imageUrl: string): Promise<void> {

        const publicId = this.extractPublicId(imageUrl);


        if (publicId) {

          await this.destroy(publicId);

        }

      }


      private extractPublicId(imageUrl: string): string {

        const parts = imageUrl.split('/');

        const fileName = parts.pop();

        const publicId = fileName?.split('.')[0];


        return publicId || '';

      }


      private destroy(publicId: string): Promise<void> {

        console.log('this is public id', publicId);


        return new Promise((resolve, reject) => {

          v2.uploader.destroy(publicId, { invalidate: true }, (error, result) => {

            if (error) {

              console.error('Error deleting resource:', error);

              reject(error);

            } else {

              console.log('Resource deleted successfully:', result);

              resolve(result);

            }

          });

        });

      }

  • Tom
    Tom Member, Cloudinary Staff Posts: 109

    Hi @Sarmad ,

    Can you share an actual public_id and your cloud_name you are deleting?

    You may be deleting a video or a raw file in which case you need to specify resource_type:video or resource_type:raw respectively, the default value is always image.

    Thanks,

    Thomas

  • Sarmad
    Sarmad Member Posts: 8

    I also tried the resource_type:image, but this also did'nt worked for me .

    public_id: gkzdhra7qwkqyuovcojc

    and one more thing i am getting this in console logging for below code

    this is public id gkzdhra7qwkqyuovcojc

    Resource deleted successfully: { result: 'not found' }

    Code:

    async deleteByUrl(imageUrl: string): Promise<void> {

        const publicId = this.extractPublicId(imageUrl);


        if (publicId) {

          await this.destroy(publicId);

        }

      }


      private extractPublicId(imageUrl: string): string {

        const parts = imageUrl.split('/');

        const fileName = parts.pop();

        const publicId = fileName?.split('.')[0];


        return publicId || '';

      }


      private destroy(publicId: string): Promise<void> {

        console.log('this is public id', publicId);


        return new Promise((resolve, reject) => {

          v2.uploader.destroy(publicId, { invalidate: true }, (error, result) => {

            if (error) {

              console.error('Error deleting resource:', error);

              reject(error);

            } else {

              console.log('Resource deleted successfully:', result);/// t his gets console log but in results it shows undefined

              resolve(result);

            }

          });

        });

      }

  • Sarmad
    Sarmad Member Posts: 8

    Thanks @Tom

  • Tom
    Tom Member, Cloudinary Staff Posts: 109

    @Sarmad Thanks for the update, glad it's working now!