info('Start Delete Old Labels Command...'); Log::channel('dailyCron')->info('Cron...Start Delete Old Labels Command...'); $labelsDirectoryTmp = '/www/sentm3live/htdocs/labels/'; $subFolders = ['CarrierLabel/', 'ContentLabel/', 'PackingList/']; // ,'CarrierLabel/','ContentLabel','PackingList','' $daysOld = 180; // Number of days to consider a file old : 180 =Six Months $filesToDeleteArr = []; foreach ($subFolders as $subFolder) { $labelsDirectory = $labelsDirectoryTmp . $subFolder; $this->info('Getting Files From Directory: ' . $labelsDirectory); Log::channel('dailyCron')->info('Getting Files From Directory: ' . $labelsDirectory); exec('find ' . $labelsDirectory . ' -ctime +180 -type f -exec rm {} \;'); } $labelsFolder = $labelsDirectoryTmp; // Get Files from the specified Directory $filesToDelete = File::allFiles($labelsFolder); foreach ($filesToDelete as $file) { $lastAccessTime = new \DateTime('@' . $file->getATime()); $now = new \DateTime; $diff = $now->diff($lastAccessTime)->days; if ($diff >= $daysOld) { $this->info('File: ' . $file . ' is Days Old: ' . $diff); // Log::channel('dailyCron')->info("File: ".$file." is Days Old: ".$diff); // Add the File to Array $filesToDeleteArr[] = $file; } } // Delete Old Files File::delete($filesToDeleteArr); Log::channel('dailyCron')->info('Cron...End Delete Old Labels Command...'); $this->info('End Delete Old Labels Command...'); } catch (\Throwable $th) { Log::channel('dailyCron')->error($th); } } }