v2.uploader.destroy function not wokring
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);
}
});
});
}
Best Answers
-
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
2 -
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 || '';
}
1
Answers
-
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
0 -
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);
}
0 -
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);
}
});
});
}
0 -
Hi @Sarmad ,
Can you share an actual
public_id
and yourcloud_name
you are deleting?You may be deleting a video or a raw file in which case you need to specify
resource_type:video
orresource_type:raw
respectively, the default value is alwaysimage
.Thanks,
Thomas
0 -
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);
}
});
});
}
0