I have a cache folder that stores html files. They are overwritten when needed, but a lot of the time, rarely used pages are cached in there also, that just end up using sp
$directory = $_SERVER['DOCUMENT_ROOT'].'/pathfromRoot/';
$files = array_slice(scandir($directory), 2);
foreach($files as $file)
{
// $extension = substr($file, -3, 3);
// if ($extension == 'jpg') // in case you only want specific files deleted
// {
$stat = stat($directory.$file);
$filedate = date_create(date("Y-m-d", $stat['ctime']));
$today = date_create(date("Y-m-d"));
$days = date_diff($filedate, $today, true);
// dd($days);
if ($days->days > 180)
{
unlink($directory.$file);
}
// }
}