Depending on the Model or upload type, and model relations, I find nesting uploads in a similar hierarchy to the models relationships makes automating deleting processes much easier. I will use model IDs as the folder name, and they may have nested folders for sub models and their uploads.
Example, Organization has many Project, and Project has many ProjectModel, which has many Render.
I may setup directories as such:
- /organizations/{id}
- avatars
- documents
- projects/{id}
- avatars
- models/{id}
- obj
- avatar
- renders/{id}
...
So if I need to simply remove a specific render and its computed files, being the furthest nested item, I would have a helper method on the Render model that would go up the chain loading the org ID, project ID, model ID, and its own ID.
public function getStorageDirectory(): string
{
$parent = $this->model->getStorageDirectory();
return "$parent/renders/$this->id";
}
Storage::deleteDirectory($render->getStorageDirectory());
But if say, an entire organization was deleted. I can simply delete the top level directory for the organization without needing to run through every model/relationship and what files they may have.
public function getStorageDirectory(): string
{
return "organizations/$this->id";
}
Storage::deleteDirectory($organization->getStorageDirectory());
Of course I can see you not wanting to nest too much, and your mileage may vary depending on your needs.