Skip to main content

How to delete all assets within a folder?

Comments

15 comments

  • Narayanan Raghavan

    Can you show us an example in Java?

    0
  • Lei Wang

    It (api.deleteResourcesByPrefix) does not work with Scala to delete a folder. Only "image" and "raw" can be defined to the resourceType. Does anyone have any idea?

    0
  • Maor Gariv

    Hi,

    Sorry for the very late reply, we have noticed that this request was left unattended. I guess this is not relevant anymore but for future reference -
    deleteResourcesByPrefix is meant to delete resources within a certain folder. Deleting the folders is currently not supported on our SDKs (it's currently on our road map). Please note that you can delete empty folders using the console.

    0
  • Amit

    works great.

    0
  • Gar

    Hello,

    Fast forward to March 2019. Is it possible to now delete the folder as well. Currently it only deletes files inside the  'mySubfolder' folder. Leaving an empty folder behind seems incomplete for what I'm trying to do.

    cloudinary.api.delete_resources_by_prefix('myMainFolder/mySubfolder', ()=>{ console.log("I want to delete mySubfolder as well") })
     
    Thank you.
     
    1
  • Stephen Doyle

    Hi Gar,

    You can make a second call to remove the folders, there's an API method for this but it doesn't currently have a corresponding helper method in the SDKs - the HTTP endpoint is an HTTP DELETE call to

    `<API key>:<API_SECRET>@https://api.cloudinary.com/v1_1/<cloud_name>/folders/<folder> `

    You can use that to delete the empty folders. There's a task on our roadmap to add this to the SDK directly, but I'm afraid I don't have an ETA for when it will be available

    0
  • Gar

    Hi Stephen,

    Thanks for getting back. I'm not very familiar with how to implement your solution. Do I do this in the Terminal? 

    I would like to perform the delete folder on the server using Node. Is that possible? If so can you provide a snippet please?

     

    Thanks,
    Gar

     

    0
  • Stephen Doyle

    Hi Gar,

    There isn't a specific method for this in our Node SDK yet, but you can call the endpoint directly using any HTTP library of your choice. 


    For example, with the 'request' module ( https://github.com/request ), you can delete an empty folder like this:

    util = require('util');
    request = require('request');

    var cloud_name = '';
    var api_key = '';
    var api_secret = '';
    var folder = 'myMainFolder/mySubfolder';

    var url = util.format("https://api.cloudinary.com/v1_1/%s/folders/%s", cloud_name, folder);

    request.delete(url, {
      auth: {
        user: api_key,
        pass: api_secret,
        sendImmediately: false
      },
      json: true
    }, function(error, response, body) {
         console.log(error, response, body);
    });


    When I tested this, the 'body; output was:

    deleted: [ 'tmp/folders/delete' ] 
    2
  • Gar

    Amazing!!!!

    Tried it and it works. Thanks so much Stephen!

    0
  • Stephen Doyle

    You're welcome; if you need anything else please let me know, and you can always open a support ticket for my team directly via http://support.cloudinary.com

    1
  • mike

    @Stephen Doyle  Thank you!  Best solid node.js answer I could find online.  Thank you again. 

    0
  • Dhyey Thumar

    Does this work for all type of assets inside a folder? Because for me it only deleted images and not the raw or video files inside the same folder.

    0
  • Zachary Gould

    @Dhyey Image will be the default and you'll need to specify for other types. Via our documentation:

    Relevant as a parameter only when using the SDKs (the resource_type is included in the endpoint URL when using the REST API). Note: use video for all video and audio assets, such as .mp3. Possible values: image (default), raw, video.

    Read More…

     

    0
  • Dhyey Thumar

    @Zachary thanks for the response, initially I thought “all” param would do the trick to remove all type of assets but ok got it that I would need to fire the query for each asset type individually.

    0
  • Zachary Gould

    No problem happy to help, also feel free to reach out via https://support.cloudinary.com/hc/en-us/requests/new if you have any additional problems

    0

Please sign in to leave a comment.