Even now, nearly 7 years after the question was asked, it seems this is impossible to do using AQL. Mainly because the AQL 'item' entity doesn't support the 'children' field, so you cannot check for folders where the children field is empty.
AQL entity documentation here: https://jfrog.com/help/r/jfrog-rest-apis/aql-entities-and-fields.
So, I solved this using a Python script that calls the the 'storage' REST API.
Apologies I cannot post the actual code here, but the abstract approach is as follows:
Make an HTTP "get" request to the Artifactory storage API specifying the parent path in your repository. To specify the root of the repo, just use "/".
Fetch the 'children' field from the result object.
If the 'children' collection exists and is not empty, then iterate through its contents checking whether each child item is a folder.
- Folder items have a boolean property 'folder' with a value of True.
- File items do not have this property.
For any child item which is a folder, recursively call steps 1-7, passing a folder URI which is the product of the current folder URI and the child item's own URI (child URI is relative to its parent)
Count the children of the current folder, taking into account that any child folders might have been deleted. I simply call the storage API again for the current folder to get a fresh view of its 'children' collection, but you could also count and pass back up the recursive call chain.
If no child items remain for the current folder, then make an HTTP "delete" for the current folder's repository and URI.
Return to the caller.
If you are ONLY cleaning up empty folders, the above can be simplified. My own implementation is more complex because I'm checking file 'lastUpdated' timestamps against a configurable retention period and optionally deleting files as well, so I keep track of both files and folders as I recurse the folder structure. This way I housekeep old artefacts, and delete any folders that are left empty as I go.