I posted before about using Cloudinary to upload images from a mobile app. I've since moved on from that solution and use a different method. Prior to the transition, I had thousands of "derived images" taking up space on Cloudinary and almost tripping me over into the much higher pricing tier.

Most of these derived images would never be seen again. They were generated when a user viewed an image in the wrong format/size. Once generated, they stay there forever taking up space. In most cases, my users would never view them again. So, I went on a mission to get rid of them.

After discovering a few bugs with the Cloudinary Node.js client, I contacted their support. The Cloudinary team fixed the problem quickly, and I was able to delete all my derived images in just a few minutes. I went from being at 99% of my current pricing tier to just 63%. This saved me $100 per month!

Here's a quick script to accomplish this. Yes, it's ugly but it worked great for the one time I needed it.

WARNING : Use at your own risk. I take no responsibility for any problems caused by this script. Review it and the documentation to make sure you understand it's workings and repercussions.

var cloudinary = require('cloudinary');

cloudinary.config({ 
  cloud_name: 'your-cloud-name-here', 
  api_key: 'your-api-key-her', 
  api_secret: 'your-api-secret-here' 
});

deleteDerived();

function deleteDerived(next_cursor) {

	try {
		var config = {
			// IMPORTANT : If you don't use this, then ALL photos will be deletd
			keep_original: true
		};

		if(next_cursor) config.next_cursor = next_cursor;

		// This will delete all derived resources up to the cursor limit of 2000
		cloudinary.api.delete_all_resources(function(result){
			
			// Just some logging to show what got deleted
			console.log("\n\n");
			console.log( new Date());
			console.log(result);

			// Wait 5 seconds to prevent exceeding your API limit
			if(result && result.next_cursor) {
				setTimeout(function() {
					deleteDerived(result.next_cursor);
				},5000)
			}
		}, config ); // Be sure to pass in the config or say Bye Bye to all your photos

	} catch(e) {
		console.log("Failed!!!");
		console.log(e);
		console.log(e.message);
	}


}