After a deployment, some pages show a blank screen or a 500 error while others work. Errors like “Class … does not exist” or “Type Error occurred when creating object” appear in the logs. The usual cause is the compiled code in the generated/ folder.
Why it happens
- Compilation was interrupted or ran out of memory. Magento creates the files before writing them, so a crash leaves some empty.
- Code changed but was not recompiled. In production mode, a changed constructor needs a fresh
setup:di:compile. - It ran as root. The
generated/folder then belongs to root, and the web server cannot write the classes it needs. - PHP still serves the old code. With OPcache timestamp checks off, PHP-FPM keeps the old compiled files until it is reloaded.
Check for empty files
find generated/code -name '*.php' -size 0 | head
How to fix it
As the user that owns the Magento files:
rm -rf generated/code generated/metadata
php -d memory_limit=4G bin/magento setup:di:compile
bin/magento cache:flush
sudo systemctl reload php8.2-fpm
If compilation reports an error, fix that first. It names the class or module at fault, and recompiling without fixing it reproduces the problem.
If you had to run it as root, give the files back afterwards:
chown -R www-data:www-data generated var pub/static
Prevent it
- Run deploy commands as the web server user.
- Give compilation enough memory (4 GB is safe for most stores).
- Make reloading PHP-FPM the last step of every deploy.
- Check
var/log/exception.logright after every deploy, not just whether the home page loads.
Catch it automatically
MageMonitor checks for this every few minutes as part of its extension and module checks, and alerts you by email, Slack or webhook the moment it appears, with the exact commands to fix it. See MageMonitor for Magento 2.
