Delete Old Files Automatically with Cron
To clean up files older than 7 days every week, combine cron with find -mtime. The key safety points: quote the path, use -type f so directories survive, and test with -print before adding -delete.
The crontab line
0 4 * * 0 /usr/bin/find /var/tmp/cache -type f -mtime +7 -delete
Runs every Sunday at 4 AM and deletes regular files last modified more than 7 days ago. -mtime +7 means "more than 7 full 24-hour periods".
Test before you delete
Always dry-run first: replace -delete with -print and check the list is what you expect. One wrong path with -delete is unrecoverable.
Delete empty directories too
Add a second pass: find /var/tmp/cache -type d -empty -delete after the file pass to clean up folders left behind.
FAQ
What cron command deletes files older than 7 days?
0 4 * * 0 find /path -type f -mtime +7 -delete runs weekly and removes regular files modified more than 7 days ago. Change +7 for a different age.
How do I test a find -delete cron job safely?
Replace -delete with -print and run it manually first. Confirm the printed list is exactly what you want gone before scheduling the -delete version.
What does -mtime +7 actually mean?
It matches files whose data was last modified more than 7 full 24-hour periods ago. -mtime 7 (no sign) means exactly 7 days; -mtime -7 means less than 7 days.