Clearing a Cache Directory Daily with Cron
Use find with -mtime to delete only files older than N days, which is safer than rm -rf on a whole directory. For application caches, call the framework's own clear command. Always use an absolute, specific path.
Delete old files safely
0 4 * * * /usr/bin/find /var/cache/app -type f -mtime +1 -delete
-type f targets files only, -mtime +1 limits to files older than a day. This won't remove the directory itself or newer entries.
Why not rm -rf
rm -rf /var/cache/app/* works but is unforgiving — a mistyped or empty variable can wipe the wrong path. find ... -delete is scoped and predictable.
Application caches
Prefer the framework command where one exists: Laravel php artisan cache:clear, or Redis redis-cli FLUSHDB. Call it with absolute paths from cron.
FAQ
How do I clear a cache directory with cron?
Use find to delete old files: 0 4 * * * /usr/bin/find /var/cache/app -type f -mtime +1 -delete. Safer than rm -rf.
Is rm -rf safe in a cron cache-clear job?
It's risky — an empty or mistyped path variable can delete the wrong directory. Prefer find with -type f -mtime and an absolute path.
How do I clear an application cache from cron?
Call the framework command, e.g. php artisan cache:clear or redis-cli FLUSHDB, using absolute paths since cron's PATH is minimal.