Deleting empty folders across 23000 folders

markstuartwalker
markstuartwalker Member Posts: 13
edited January 12 in Developer APIs

I have my assets grouped 9 per asset folder and some 23,000 asset folders. I want to delete any asset folder which contains no assets.

I have developed a purge utility which recursively traverses the asset folders and does what I need. HOWEVER when I run this in my production environment it hit the 5000 calls/h limit once it has traversed only a fraction of the folders. This is due to calling ListResourceByAssetFolderAsync() once per folder

Is there an API call which can find empty folders more efficiently?

Thanks

Mark

Tagged:

Answers

  • Cloudinary Team
    Cloudinary Team Administrator, Cloudinary Staff Posts: 170 admin

    Hi there,

    Thanks for reaching out.

    You can use our Search API to check if there are any assets in a folder as well as its subfolders by using the following REST API query:

    /resources/search?expression=”folder:search_test/*”&max_results=500
    

    This call looks at the folder called "search_test" as well as its subfolders and returns the count of how many assets it found so you will know if any assets are located in these folders or not. If no assets were found, the folder and all subfolders are empty and can be deleted.
    You can find the relevant documentation on the Search API here:
    https://cloudinary.com/documentation/search_method#search_method

    I hope this helps, please let me know if you have any further questions.

    Kind regards,

    Tia

    Helpful Links For You
    💬 Share questions, connect with other users in our Cloudinary Community forums and Discord server!
    🧑‍🎓 Join our Cloudinary Academy for free courses, workshops and other educational resources.
    📄 Read our documentation for in-depth details on Cloudinary product features and capabilities
    📰 Check out the Cloudinary blog for the latest company news and insights

  • markstuartwalker
    markstuartwalker Member Posts: 13

    Sorry but the search function allows you to find assets with certain attributes. It does not find folders which contain no assets.

  • Cloudinary Team
    Cloudinary Team Administrator, Cloudinary Staff Posts: 170 admin

    Hi there,

    We do not have an out-of-the-box way to do this. However, I did find this code snippet that might help. I have not personally tried it, but from a cursory glance, I believe it does what you are looking for.

    import cloudinaryfrom cloudinary import apicloudinary._config._parse_cloudinary_url("ccloudinary_url")def find_end(root, f):root = root.replace(" " , "%20")res = list(filter(lambda x: x != 'search', list(map(lambda x: x['path'], api.subfolders(root)['folders']))))tmp = [find_end(i, f) for i in res] if res != [] else f.append(root)if root == "": return fempty = list(filter(lambda x: cloudinary.Search().expression("folder:{}".format(x)).execute()['total_count'] == 0, find_end("", [])))print("Empty folders:\n{}".format(empty))
    

    I hope this helps. Let me know if you have any questions.

    Kind regards,

    Tia

    Helpful Links For You
    💬 Share questions, connect with other users in our Cloudinary Community forums and Discord server!
    🧑‍🎓 Join our Cloudinary Academy for free courses, workshops and other educational resources.
    📄 Read our documentation for in-depth details on Cloudinary product features and capabilities
    📰 Check out the Cloudinary blog for the latest company news and insights