private final static void removeFolders ( String folder , List < String > folderTemplates , int numberOfDaysToKeepTempFolders ) { long dateToRemoveFiledAfter = ( new Date ( ) ) . getTime ( ) - ( numberOfDaysToKeepTempFolders * MILLISECONDS_IN_DAY ) ; File tempFolder = new File ( folder ) ; File [ ] folderChildren = tempFolder . listFiles ( ) ; if ( null == folderChildren ) { log . debug ( "Folder '" + tempFolder . getName ( ) + "' was empty. Nothing to delete" ) ; return ; } for ( File currentFile : folderChildren ) { if ( currentFile . isDirectory ( ) ) { for ( String folderTemplate : folderTemplates ) { if ( currentFile . getName ( ) . contains ( folderTemplate ) && ( currentFile . lastModified ( ) < dateToRemoveFiledAfter ) ) { try { currentFile . delete ( ) ; FileUtils . deleteDirectory ( currentFile ) ; log . debug ( "Folder '" + currentFile . getName ( ) + "' deleted..." ) ; } catch ( Exception e ) { log . fatal ( "Error deleting folder '" + currentFile . getName ( ) + "'" ) ; } } } } } }
This method can be called to remove specific folders or set how long you want to keep the temp information .