Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove images from Cloudinary by Node.js Express?

Upload and set default controller functions are working perfectly. However, we are trying to implement delete image from Cloudinary as well. How can it be done? In the documentation it was confusing. Here is the code:

 const cloudinary = require('cloudinary');
    const HttpStatus = require('http-status-codes');

    const User = require('../models/userModels');

    cloudinary.config({
      cloud_name: 'name',
      api_key: 'key',
      api_secret: 'secret'
    });

    module.exports = {
      UploadImage(req, res) {
        cloudinary.uploader.upload(req.body.image, async result => {
          await User.update(
            {
              _id: req.user._id
            },
            {
              $push: {
                images: {
                  imgId: result.public_id,
                  imgVersion: result.version
                }
              }
            }
          )
            .then(() =>
              res
                .status(HttpStatus.OK)
                .json({ message: 'Image uploaded successfully' })
            )
            .catch(err =>
              res
                .status(HttpStatus.INTERNAL_SERVER_ERROR)
                .json({ message: 'Error uploading image' })
            );
        });
      },

      DeleteImage(req, res) {
    cloudinary.uploader.destroy(req.params.image, async result => {
      await User.update(
        {
          _id: req.user._id
        },
        {
          $pull: {
            images: {
              imgId: result.public_id,
              imgVersion: result.version
            }
          }
        }
      )
        .then(() =>
          res
            .status(HttpStatus.OK)
            .json({ message: 'Image deleted successfully' })
        )
        .catch(err =>
          res
            .status(HttpStatus.INTERNAL_SERVER_ERROR)
            .json({ message: 'Error deleting image' })
        );
    });
  },

      async SetDefaultImage(req, res) {
        const { imgId, imgVersion } = req.params;

        await User.update(
          {
            _id: req.user._id
          },
          {
            picId: imgId,
            picVersion: imgVersion
          }
        )
          .then(() =>
            res.status(HttpStatus.OK).json({ message: 'Default image set' })
          )
          .catch(err =>
            res
              .status(HttpStatus.INTERNAL_SERVER_ERROR)
              .json({ message: 'Error occured' })
          );
      }
    };

We are using Node.js Express with Mongoose. How can we include extra function here that will remove images?

like image 618
NewTech Lover Avatar asked Oct 20 '25 14:10

NewTech Lover


2 Answers

There are two options to delete an image from cloudinary:

  1. By using the admin API. For example in Node:

cloudinary.v2.api.delete_resources(['image1', 'image2'], function(error, result){console.log(result);});

  1. Using our upload API:

cloudinary.v2.uploader.destroy('sample', function(error,result) { console.log(result, error) });

Please note that using our admin API is rate limited and you might want to use the second option.

like image 197
Shirly Manor Avatar answered Oct 23 '25 04:10

Shirly Manor


it just because your req.params.image is like https:https://res.cloudinary.com/your/image/upload/v1663358932/Asset_2_bdxdsl.png

instead write your delete request like so :

cloudinary.v2.uploader.destroy('Asset_2_bdxdsl', function(error,result) {
   console.log(result, error) }) 

ps: Asset_2_bdxdsl is your image name without prefix .png !!

like image 41
Compty Adel Avatar answered Oct 23 '25 03:10

Compty Adel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!