Symfony 3.4缓存目录未自动清理引发磁盘空间不足的技术问询
Hey there! Let's break down your questions about the growing /var/www/project/var/cache/dev directory in Symfony 3.4:
1. Does the cache directory auto-cleanup when hitting a disk threshold?
Short answer: No, Symfony 3.4's dev environment cache does NOT automatically clean itself based on disk space usage.
Here's why: The dev cache is built for flexible development workflows—every time you tweak code, templates, or configuration, Symfony generates new cache files to reflect those changes. Old cache directories (labeled with unique hashes) aren't removed automatically by default. Over weeks or months of development, this unused cache buildup eats up your disk space quickly.
Note: This differs from the production environment, where cache is usually generated once and only refreshed when you manually clear it or use targeted cache pool invalidation. Dev mode prioritizes development convenience over automatic cleanup.
2. Can adding a cache cleanup command to Cron solve the problem quickly?
Absolutely! This is a straightforward, reliable fix for your disk space crunch. Here's how to set it up:
Step 1: Use the Symfony dev cache clear command
The core command to wipe the dev cache is:
php bin/console cache:clear --env=dev
If you don't need to pre-warm the cache (which speeds up future requests but isn't mandatory for dev), add the --no-warmup flag to make the command run faster:
php bin/console cache:clear --env=dev --no-warmup
Step 2: Schedule the command with Cron
Open your crontab editor using:
crontab -e
Add a line to run the cleanup on a schedule that matches how fast your cache grows. For example, to run it daily at midnight:
0 0 * * * cd /var/www/project && php bin/console cache:clear --env=dev
If your web server runs under a specific user (like www-data), run the Cron job as that user to avoid permission conflicts:
0 0 * * * www-data cd /var/www/project && php bin/console cache:clear --env=dev
Adjust the schedule (the first five time fields) if needed—you could run it hourly (0 * * * *) if your cache grows extremely quickly.
Bonus Tips
- After the Cron job runs, check
/var/www/project/var/cache/dev—you should only see the latest cache hash folder, confirming old cache was removed. - For a more granular cleanup, you could write a script to delete cache directories older than X days, but
cache:clearis the most Symfony-native and reliable method.
内容的提问来源于stack exchange,提问作者shuba.ivan

