based on the file timestamp, or do you have a database record that tells you when the file was uploaded?
Laravel Schedule To Remove Files Older Than...
Morning All
I've recently setup some commands & schedules however one that i need to setup, is to remove user uploaded files older than 15 days old.
I've had a google around and i can't seem to find what i'm looking for and cant figure out how to do it.
Please could one of you kind folk advise how best to carry this out?
Thanks in advance.
Unless you specify a different path (or disk), the path would be the root directory as specified by the default disk/driver in config/filesystems.php or FILESYSTEM_DRIVER in your .env file.
If everything is left at the defaults this will just return whatever is in your /storage/app folder.
If you want it to list the contents of subfolders, such as userUploads you'll need to tell it so by passing true for the $recursive parameter on Storage::listContents(). The first parameter allows you to specify a path that is appended to the specified disk.
Example based on @Snapey's above:
collect(Storage::disk('public')->listContents('userUploads', true))
->each(function($file) {
if ($file['type'] == 'file' && $file['timestamp'] < now()->subDays(15)->getTimestamp()) {
Storage::disk('public')->delete($file['path']);
}
});
This would collect the files in your /storage/app/public/userUploads directory (and in any subfolders) and delete them if they are a.) a file and b.) older than 15 days.
Please or to participate in this conversation.