A Clean Slate: Clearing Magento 2 Generated and Cached Files
When Magento 2 behaves strangely after a deploy, stale generated files are a common cause. This command clears them while protecting what should stay.
What it does
- Empties
var, exceptvar/sessionand its.htaccess. - Empties
pub/static, except its.htaccess.
The command
Run it from the Magento root:
find var/* -type f -or -type d | grep -v 'session' | xargs rm -rf && \ rm -rf pub/static/*
Then redeploy static content and flush the cache. Avoid running it on a busy production store during trading hours.
Need a second pair of eyes on this? Our team handles Magento development every week. See how we can help.