# PHP: Recursively Delete Old Files

I have a folder named "storage". There are log files and temporary cache files created within hierarchy of subfolders. I need to traverse through all subfolders and delete files older than 6 months (based on modified time).

%[https://gist.github.com/hongster/6eacdbb4c08a8efe0a1aac720d662c6d]

`$timeThreshold = strtotime("-6 months");`

Files last modified before this timestamp will be deleted.

`$iterator = new RecursiveDirectoryIterator($folder);`

Using [RecursiveDirectoryIterator](https://www.php.net/manual/en/class.recursivedirectoryiterator.php) for recursive folder traversal. `listDeletableFiles` function can be customize to filter files for deletion. For example, you may want to delete files with ".log" extension.
