Using Admin API
The public ID of an asset which is located in a sub-folder will contain the full folder path, using slashes (`/`) as delimiters. For example: cars/Honda/Accord
Therefore, by filtering for only assets which don't contain slashes in their public ID, you can list all the assets which are currently in your account's root folder.
For example in Rails:
results = Cloudinary::Api.resources(type:"upload")
resources = results["resources"]
ids = resources.map {|res| res["public_id"]}
ids.select! {|public_id| !public_id.include?("/")}
The array ids
will now contain the exact list of assets in the root folder we queried for.
Further actions can now be taken with the resources in question (e.g. deleted, relocated, etc.).
Using Search API
The Search API allows you to search for folders directly. You can list all the assets which are currently in your account's root folder by using the following search expression:
folder=""
For example in Rails:
result = Cloudinary::Search
.expression('folder=""')
.sort_by('public_id','desc')
.max_results(30)
.execute
This will list any assets in your root folder (images, videos, and raw files).
Comments
2 comments
This seems to be listing all images that have ever been uploaded. How would I modify to lust current images (ie exclude images that I've deleted).
Hi Ben,
If you have backup enabled then this would also list images that are placeholders (i.e has the parameter `placeholder` set to `true`). We use place holder images so we could later retrieve them from backup when needed.
Filtering those using our API is possible using our search API which is available from our advanced extra plan and above. Otherwise, you could filter these programmatically from the response received.
Please sign in to leave a comment.