I'm trying to construct a custom artisan command which cleans up my filesystem each day, there are a few steps to it and I cant figure out a way to do it without crashing the memory. There are thousands of folders and hundreds of thousands of users.
What i need to do is;
- Get all folders that are older than 50 days (based on folder structure below)
- From those collected folders, query the user id (which is the nested folder name below) and check if the data has been completed
$user->isCompleted() - If that users data is completed, then delete the directory which stores that users data
In my filesystem i have stored data for each user id based on day, so the filesystem looks like so;
data/2022-01-15/7482947
data/2022-01-15/7482946
data/2022-01-15/7482945
data/2022-01-16/2353234
data/2022-01-16/2353233
data/2022-01-16/2353232
The format is;
data/<date>/<user_id>
So far I have managed to return the folders which are older than 50 days, using the code below, but iam unsure of how to continue getting the nested folder and then querying the DB
collect(Storage::directories('data/'))
->filter(function ($directory) {
$directoryDate = Str::after($directory, '/');
if (! Carbon::parse($directoryDate)->lte(now()->subDays(50))) {
return false;
}
return true;
});
Any help would be greatly appreciated.