Magento2 Regenerate full static content
-
28-09-2020 - |
Question
I removed the folder pub/static while in developer mode, expecting that things will generate back to the way they were. After that though, only the folder
_requirejs
got generated, so now I am missing everything - all css and js files are gone. How can I fix that problem ? Please, help.
Solution
You can regenerate your static content by running the following command
php bin/magento setup:static-content:deploy
This is the command you'd run before deploying to production mode. If you're running in default or developer mode, those files should be generating for your automatically. My guess is you accidentally removed a folder Magento expects to be there -- running setup:static-content:deploy
may restore that folder, or may result in an explicit error about the missing folders. If it's the later, the error should (hopefully!) provide enough information to rebuild the missing directories. You may want to run the command in verbose mode
php bin/magento -vvv setup:static-content:deploy
This will tell the bin/magento
command to output the actual exception if there's an error.
OTHER TIPS
What caused this is that when you removed the pub/static folder itself, you removed an .htaccess file that is necessary for regenerating content while in production mode. Without that file, no assets will be automatically regenerated.
If this ever happens again, you could copy that file from another Magento 2 instance (if you have another one setup) or try to restore the file by copying the .htaccess from Magento's GitHub: https://github.com/magento/magento2/blob/develop/pub/static/.htaccess
To prevent this from happening, only empty the contents of a folder under the pub/static directory, such as frontend or adminhtml.
You just need to run this command to generate static content:
php bin/magento setup:static-content:deploy
Make sure the owner of pub static is same as your Magento owner.
Make sure you have the required .htaccess file in your folder.
- Remove all folders under
pub/static
- Remove
var/cache
- Remove
var/composer_home
- Remove
var/generation
- Remove
var/page_cache
- Remove
var/view_preprocessed
After this, go to the root of magento 2 and run:
php bin/magento setup:static-content:deploy
You can follow the below steps:-
1. Delete all folders under pub/static
2. Delete var/cache
3. Delete var/composer_home
4. Delete var/generation
5. Delete var/page_cache
6. Delete var/view_preprocessed
Now open your command prompt and go to your root installation of magento 2 and run the following commands
php bin/magento setup:static-content:deploy
If you removed the .htaccess file you could have lots of problems. (getting 404 errors when trying to access your css + javascript files and whatnot.
If this is happening to you the best way I found of fixing this is re-downloading the magento zip or tar and extracting the pub/static folder to your project directory. This should include the .htaccess file and fix any problem you have.