'Delete image from Clodinary using axios delete
I am uploading an Image on cloudinary using axios post request and I also want to delete images using axios delete but I couldn't find any api url or any code that can help me to send delete request to cloudnary using axios delete.
Using This code to upload image on Cloudinarylet imageFile = this.props.img
var formData = new FormData()
formData.append('file', imageFile)
formData.append('upload_preset', Routes.CLOUDINARY_PRESET)
await axios.post(Routes.CLOUDINARY_API, formData)
.then(function (res) {
imageURL = res.data.secure_url
})
.catch(function (err) {
console.log("err", err)
})
Solution 1:[1]
To delete an Image from cloudinary using axios post, first you have to create a signature. Click here to see the documentation to create the signature. Also you will need an library to hash your string to hash1 format. In this example I have used "crypto-hash" library.
In addition to that you will need other parameters such as public_id, api_key and a timestamp.
**Note - Your API_Key and API-Secret can be found in your cloudinary account dashboard. After getting all required data, then you can use below code to send the API request. Public_id is an unique id to clearly identify the image we need to delete from cloudinary media library.
const timestamp = new Date().getTime()
const string = `public_id=${<Add public_id>}×tamp=${timestamp}<add your api secret>`
const signature = await sha1(string)
const formData = new FormData()
formData.append("public_id",<public_id>)
formData.append("signature",signature)
formData.append("api_key",<your api_key>)
formData.append("timestamp",timestamp)
const res = await ax.post("https://api.cloudinary.com/v1_1/<your cloud name>/image/destroy", formData)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Supuna Warusawithana |
