When deleting a Document, the async listener OrphanVersionRemoverListener will run and according to VersionRemovalPolicy this listener may remove the orphan versions if they are not filtered by a contributed OrphanVersionRemovalFilter.
For recursive deletes (i.e. folder delete with children) only the first level of deletion is handled at application level, the subtree recursion is handled at SQL level using a ON DELETE CASCADE trigger.
Because versions don't have a parentId (unfiled), they are not deleted by the trigger.
Handling configurable logic at SQL level is not possible.
On the other hand, managing the recursion at Java level is far more costly.
A quick solution is to provide a cleanup stored procedure and use a scheduler to run it periodically.
The solution chosen to solve this issue is to add a scheduled cleanup mechanism. The cleanup mechanism does periodic commits, every 1000 documents by default. This value can be changed using the Configuration Property org.nuxeo.orphanVersionsCleanup.commitSize.
In hotfixes version for Nuxeo 8.10 and Nuxeo 7.10, it's not enabled by default, and must be enabled by an explicit contribution: