Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ollie_123's avatar

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.

0 likes
10 replies
Snapey's avatar

based on the file timestamp, or do you have a database record that tells you when the file was uploaded?

ollie_123's avatar

Hey @snapey

Thanks for the quick response. It would be based on the file timestamp...

ollie_123's avatar

Would it be via the storage Facade? I cant see that you can use a where clause on it though?

Snapey's avatar

using the flysystem api you can get a collection of files, then iterate through them and check the timestamp, rejecting all those not matching the criteria.

https://flysystem.thephpleague.com/v1/docs/usage/filesystem-api/

The following returns the contents of the app folder as an array (assuming default disk)

Storage::listContents('');

within each item of the collection is a timestamp property, you can compare this to your cutoff date and delete the file if required.

collect(Storage::listContents())
  	->each(function($file) {
      
      	if($file['timestamp'] < now()->subDays(15)->getTimestamp()) {
          
    	    Storage::delete($file['path']);
          
        }
    });

You will need to adapt the path according to where your files are located

ollie_123's avatar

Hey @snapey

Thanks for the assistance.

As you're calling storage i presume the path would be the folder inside of the public folder such as ['userUploads/']

Snapey's avatar

the config file filesystems.php configures your disks. Without specifying anything different, the storage commands assume the default disk, which would be storage/app

1 like
Dallin's avatar
Dallin
Best Answer
Level 6

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.

7 likes
Snapey's avatar

need to again specify the disk on the delete command

ollie_123's avatar

Hey @snapey @dallin Thank you both for your answers. They were exactly what i was looking for.

If i could mark both answers as best answer i would.

Really appreciate your help. Thank you.

Please or to participate in this conversation.