If you're looking for a solution that will delete all files/folders, not just "empty out" the folders, here's something I came up with that can be remixed for other use-cases.
/**
* cleanCypressBuildCache will clean up build files that Cypress generates
* before running tests. These are normally self-cleaned but if the build is interrupted
* or dies during Cypress tests, then they'll remain forever. So we clean them up here.
*/
task cleanCypressBuildCache(type: Delete) {
def cypressBuildFolder = System.getenv('APPDATA') + '/Cypress/cy/production/projects'
def daysToKeepOldItems = 2
description = 'Tidy up Cypress ' + cypressBuildFolder
// Delete old folders / files
def cutoff = new Date().minus(daysToKeepOldItems);
def deleteList = []
fileTree (dir: cypressBuildFolder).visit {
def File file = it.file
def shouldBeDeleted = new Date(file.lastModified()).before(cutoff)
if (shouldBeDeleted) {
deleteList << file
}
}
// Not sure why, by `it.delete()` didn't work but `project.delete(it)` did.
deleteList.each { project.delete(it) }
}