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

APPLE199's avatar

Removing user-uploaded files that are older than 7 days

Hey guys. I have a website where users are able to upload files. I'd like to automatically delete files that are older than 7 days. The file information such as the name and path of file is stored in database and the files are stored in the storage directory.

I was thinking of checking for files that were uploaded more than 7 days ago and deleting them manually when the user makes request to my site (by placing the code in a helper and calling the helper from all my controllers). However, this can make my site load very slowly.

So what is the best way to take care of this job? I'm not familiar with jobs and queues with laravel so it would be pretty confusing for me.

I have the code ready, I just need a best way to do it. Therefore, I'm kindly asking for your input as to what you guys think.

Cheers!

0 likes
4 replies
jlrdw's avatar

The helper is a good idea I would simply use a date/time field then you could search on that field if older than 7 days.

martinbean's avatar

@rishabswift Have a scheduled Artisan command that is ran daily to clear uploaded files older than a week:

$schedule->command('your-command-name')->daily();

You’re right in that having a helper method that’s called as part of a controller action is inefficient. You don’t want to make users wait for a page to finish loading because you’re “piggy-backing” on it trying to delete a lot of files in the background.

jekinney's avatar

Lots of videos on events. If you needed to stick with the controller triggering the check at least fire a event. Queued event even if database should be better and very simple.

The artisan command imo would be the preferred method but depending how many files per day (like over a 100) you still may want to queue.

Like I said a basic set up really isn't hard and worth a few hours learning it.

Please or to participate in this conversation.