Saturation of the cache directory: The cache files take too much disk space
Issue
With the File cache driver, the /storage/framework/cache/data/ directory grows every day and ends up taking several gigabytes (up to tens of gigabytes on busy websites), filling up the disk space of the hosting.
Cause
With the File driver, each cached item is a file. When some data is modified, the related cached items become outdated and are replaced by new files. Two things made the outdated files pile up:
- Up to LaraClassifier v19.1.3 and JobClass v19.1.3, each listing visit (to update the visits counter) and each user login refreshed a large part of the cache, and the outdated files were only deleted once expired. On a busy website, that means several gigabytes of new files per day.
- The expired files are deleted by the daily Cache Purge task of the scheduler. If the cron job is not configured, this task never runs and the expired files are never deleted.
Since LaraClassifier v19.1.4 and JobClass v19.1.4, visits and logins no longer refresh the cache, and the outdated files are deleted as soon as the cached data changes. The cron job is still required to delete the expired files.
Since LaraClassifier v19.1.5 and JobClass v19.1.5, the listing pages and the searches store much less cache data, and the expired files are also deleted without a cron job: when the daily clean up hasn't run for more than a day, short clean ups (about 1 second each, after the page is sent to the visitor) run after some visits. A warning is also displayed under Admin panel → Settings → Optimization when the cron job doesn't seem to run. The cron job is still recommended: the other scheduled tasks (listings purge, saved-search alerts, backups, etc.) need it.
Solution
- Update your website to LaraClassifier v19.1.5 / JobClass v19.1.5 or later.
- Clear the cache to get the disk space back: Admin panel → System Info → Clear Cache, or over SSH, from the website's root folder:
This is safe: the cache is rebuilt automatically as the pages are visited. If the directory is really huge and the command times out, delete the content ofphp artisan cache:clear/storage/framework/cache/data/by FTP or SSH (keep thedatafolder itself):find storage/framework/cache/data -mindepth 1 -delete - Configure the cron job, so that the expired cache files are deleted every day: see Configuring the Cron Job. Then check that it works: over SSH,
php artisan schedule:listshows the scheduled tasks, andphp artisan expired-cache:purgeruns the Cache Purge immediately and reports what it deleted.
To check the size of the cache directory over SSH:
du -sh storage/framework/cache/data
Recommendations
- If Redis is available on your server, select the
Redisdriver under Admin panel → Settings → Optimization → Data Caching System: it is faster, uses no disk space, and manages the cache expiration by itself. - With the
Filedriver, don't set a very long Cache Expiration Time (same settings page): the longer the expiration, the more files are kept on the disk. Between3600(1 hour) and86400(24 hours) seconds is a good range for most websites. - Don't run the cron job as
root: the cache files it creates couldn't be deleted by the web server afterwards.
NOTE: The sessions files are stored in another directory (/storage/framework/sessions/). If that directory is the one growing, see Saturation of the sessions directory.