Deleting all images from cloudinary through API

|
| By Webner

Deleting all images from cloudinary through API

Cloudinary is a SaaS technology company that provides a cloud-based image and video management solution. Cloudinary API provides us the facility to delete maximum 1000 resources at once. But we may have more than 1000 images in our cloudinary folder, in such a case it will not be possible to delete all those images at once.

For this purpose we can use ‘next_cursor’ attribute that comes in response of delete API but only if there are some resources left to delete in an API call.
Delete image from cloud

In the function where you want to delete the resources, place the code below (written in Laravel framework):

$delete_link='Public ID of the folder from where you want to delete resources';		$next_cursor=ControllerName::delete_from_cloudinary($delete_link,'');
while($next_cursor!='' && $next_cursor!=null){
$next_cursor=MyController::delete_from_cloudinary($delete_link,$next_cursor);
}

Here,
$delete_link will contain the public id of the folder or a particular resource to delete.

Example:
Employee Manager/’.$department.’/’.$employee[‘name’].’/’.$image_name; //Dynamic
Employee Manager/Software Engineering/Komal/komal_img.jpg;

$next_cursor will receive the return value from the called static function ‘delete_from_cloudinary’ of ‘MyController’ that will take $delete_link as its first parameter and ‘’ for second parameter because initially we do not have ‘next_cursor’ value to pass as second parameter.

while($next_cursor!=” && $next_cursor!=null){
$next_cursor=ControllerName::delete_from_cloudinary($delete_link,$next_cursor);
}

In this above loop, we are checking for ‘next_cursor’ attribute i.e if the API response contains ‘next_cursor’ attribute we will call the API again and again but each time by passing newly returned value of ‘next_cursor’ attribute. In this way, it will delete the resources left in that particular folder or with this particular public id.

Now in the function ‘delete_from_cloudinary’ in ‘ControllerName’ Controller called above we will have the following code:

public static function delete_from_cloudinary($delete_link,$next_cursor){
\Cloudinary::config ( array (
		"cloud_name" => "CLOUD NAME",
		"api_key" => "API KEY",
		"api_secret" => "API SECRET KEY"
	) );
	$api = new \Cloudinary\Api();
	$result_arr = $api->delete_resources_by_prefix("$delete_link",array('next_cursor' => $next_cursor));
	$result_array = json_decode(json_encode($result_arr), true);
	if (array_key_exists("next_cursor",$result_array)){
		$next_cursor=$result_array['next_cursor'];
	}else{
		$next_cursor='';
	}
	return $next_cursor;
}

In above code, we have following steps:
1. We will provide configuration for cloudinary to use in API which will be done through \Cloudinary::config ( ) function. It will include Cloudinary cloud name, API key and API secret key.
2. We will create an object of Cloudinary API class to use its methods in API.
$api = new \Cloudinary\Api();
3. $api->delete_resources_by_prefix(“$delete_link”,array(‘next_cursor’ => $next_cursor));

This above line will call delete_resources_by_prefix method of Cloudinary API that will delete the resources with the public id containing the value in $delete_link variable. Example :
$api->delete_resources_by_prefix(“Employee Manager/Software Engineering/Komal/”,array(‘next_cursor’ => $next_cursor));
This statement will delete all the resources in ‘Komal’ folder of this specified public id.

In this function we will also pass an array as the second parameter. This second parameter array will contain the value of “’next_cursor’”.

‘next_cursor’ is the attribute that comes in response from Cloudinary API on deletion only if some resources are left to delete. As cloudinary API has the limit to delete maximum 1000 resources at once so after deleting 1000 resources, it sends an attribute ‘’next_cursor’’ in its response that will provide us a value to use next time so that the resources can be deleted fully by passing that ‘next_cursor’ value next time.

4. $result_array = json_decode(json_encode($result_arr), true);
This line will convert the response from cloudinary to array to get the value of ‘next_cursor’ attribute.

5. if (array_key_exists(“next_cursor”,$result_array)){
$next_cursor=$result_array[‘next_cursor’];
} else {
$next_cursor=”;
}

In this if condition we are checking if the response array contains ‘next_cursor’ attribute as key or not. This is mandatory as we do not get ‘next_cursor’ each time. We get this attribute only if there are some resources left to delete because of API 1000 resources restriction. If there are some resources left then we are assigning ‘next_cursor’ value to $next_cursor variable and returning that to use again.

NOTE:
We also have a ‘partial’ attribute that comes in response from the deletion API having value true or false. If the ‘partial’ attribute is true that means the deletion was done partially because of 1000 resources restriction. If the ‘partial’ attribute value is false that means all the resources were deleted because they were less or equal to 1000.
But with ‘partial’ attribute we can only check if the deletion API deleted all resources or not. It will not help to delete further left resources. We can use this attribute just to check the status of deletion.
Delete image from cloud

Leave a Reply

Your email address will not be published. Required fields are marked *