I have installed Magento 2 and set developer mode, then I have installed one module that contains css and js files. The module works fine and css and js files are rendering from pub/static folder. Now I have made css changes in module directory css file and run command
php bin/magento setup:static-content:deploy
but Magento is not making my new changes in pub/static css files so I'm getting old css file contents. Does anyone know how to regenerate the whole css file in pub/static folder?
18 Answers 18
Before deleting pub/static make a backup of pub/static/.htaccess and restore it again. otherwise your all css and js will show 404 error!
- Remove
pub/static(keep the .htaccess backed up and copy it again) - Remove
var/cache - Remove
var/composer_home - Remove
var/generation - Remove
var/page_cache - Remove
var/view_preprocessed - Run
php bin/magento setup:static-content:deploy
-
67M2 doesn't make things very developer friendly.Matthew McLennan– Matthew McLennan2016年02月19日 18:43:22 +00:00Commented Feb 19, 2016 at 18:43
-
15@CarComp no files should ever have to be deleted manually. magento should take care of that. it is very annoyingClaudiu Creanga– Claudiu Creanga2016年06月03日 10:35:53 +00:00Commented Jun 3, 2016 at 10:35
-
14U must be new to magento ;)CarComp– CarComp2016年06月03日 11:46:37 +00:00Commented Jun 3, 2016 at 11:46
-
6I think removing pub/static you lose also the .htaccess file inside that is important to avoid 404 errors, is ok to delete the subfolders of pub/static but that .htaccess should be preservedElio Ermini– Elio Ermini2016年09月08日 09:42:02 +00:00Commented Sep 8, 2016 at 9:42
-
3I am new to "magento". WAT? Is it the official way to update css?puchu– puchu2017年03月01日 10:08:55 +00:00Commented Mar 1, 2017 at 10:08
answers here don't mention that you shouldn't delete the .htaccess in the pub/static folder.
to delete all files except the .htacces enter the pub/static directory and run
cd ./pub/static/
find . -depth -name .htaccess -prune -o -delete
cd ..
cd ..
rm -rf var/cache/ var/generation/ var/page_cache/ var/view_preprocessed/
It's important that you go to ./pub/static before running the other commands, otherwise all your directories will be deleted!
-
why regenerate with different version??? I have an issue with deploy file version not match. magento.stackexchange.com/questions/94502/…Magento2 Devloper– Magento2 Devloper2017年02月20日 08:15:25 +00:00Commented Feb 20, 2017 at 8:15
-
1did not work as expecteddChristophe Ferreboeuf– Christophe Ferreboeuf2017年05月09日 11:28:38 +00:00Commented May 9, 2017 at 11:28
-
2this command delete all of my directories -_-Izhar Mehdi– Izhar Mehdi2020年02月05日 13:45:38 +00:00Commented Feb 5, 2020 at 13:45
-
@IzharMehdi did you enter the pub/static directory like it said in the answer?Claudiu Creanga– Claudiu Creanga2020年02月05日 14:51:55 +00:00Commented Feb 5, 2020 at 14:51
-
2I deleted all files in prod website, didn't enter pub/static directory please add command cd pub/static at the beginningtarek fellah– tarek fellah2020年10月30日 16:57:31 +00:00Commented Oct 30, 2020 at 16:57
I have just delete the css files from pub/static folder and run the command:
php bin/magento setup:static-content:deploy
It will re generate the same file with latest changes.
-
why regenerate with different version??? I have an issue with deploy file version not match. magento.stackexchange.com/questions/94502/…Magento2 Devloper– Magento2 Devloper2017年02月20日 08:14:37 +00:00Commented Feb 20, 2017 at 8:14
Simple way 1
In the Magento Admin. Go to System > Tools > Cache Management and click Flush Static Files Cache.
Simple way 2
If you just changed the css file which already existing, what you need to do is just delete the corresponding file in pub/static, then when you refresh the page, it will generate the new version css file. It works in the developer mode.
Notice
When you create new file, rather than modifying an existing file, you may also need to clear the cache,
php bin/magento cache:flush.Default mode and developer mode doesn't need deploy static view files, because of static files being dynamically generated rather than materialized. (Reference: About Magento modes)
In production mode, you need deploy static files and clean cache by running
bin/magento setup:static-content:deployandbin/magento cache:flush.
-
it's not working for meSarvesh Tiwari– Sarvesh Tiwari2020年04月04日 15:52:14 +00:00Commented Apr 4, 2020 at 15:52
There is one important note to make about this question that most of the answers don't specifically point out. In your case (with the set up you are using) the reason your CSS changes aren't getting picked up when you regenerate the CSS into the pub/static folder is you aren't removing the content of the var/view_preprocessed director. Inside this directory there are cached version of your CSS that are pulling into the pub/static folder when you run the php bin/magento setup:static-content:deploy command.
So when you recompile, Magento will first look to the var/view_preprocessed folder for the cached CSS. If this folder is empty, it will look to the theme files and pull that CSS for compilation.
These setting are configurable, so there are a lot of way to get to your destination that will change the path you need to take. But for the solution to your specific setup:
Remove the files from the
pub/staticfolder:rm -rf pub/static/*Remove the files from the
var/view_preprocessedfolder:rm -rf var/view_preprocessed/*Recompile the pub static folder:
php bin/magento setup:static-content:deployClear cache is it's turned on:
php bin/magento cache:cleanRefresh the browser.
-
1would be nice if magento2 would make symlinks to those affected files in developer mode, then changes in those module files where visible on the flyroman204– roman2042018年01月25日 08:06:47 +00:00Commented Jan 25, 2018 at 8:06
-
It does, but i haven't gotten my head around the when and the how, os i didn't bring this up in the answer. There are global config files that can set this in your di.xml (
app/etc/di.xml) file. But i have had mixed results while testing. There is a little more here in the second answer: magento.stackexchange.com/questions/116605/…circlesix– circlesix2018年01月25日 14:04:46 +00:00Commented Jan 25, 2018 at 14:04
As the question is for js and css update in Magento 2 in developer mode.
- Updating the JS in your respected module,before refreshing page the same file from
pub/static/frontend/namespace/theme/ ... module/js/...delete it so after page refresh fresh JS file will generate in pub folder. - Updating the CSS in your respected module, do the pt 1.
- Updating Less file
Setup theme in magento ...\dev\tools\grunt\configs\themes.js
Clone any one theme example luma theme to your theme and assign a name suppose xyz
xyz: {
area: 'frontend',
name: 'Magento/luma',
locale: 'en_US',
files: [
'css/styles-m',
'css/styles-l'
],
dsl: 'less'
}
In root folder change file name : Gruntfile.js.sample to Gruntfile.js
In root folder change file name : package.json.sample to package.json
Need to have grunt in you site folder else install via npm
Open command prompt with administrator permission
Go to site folder in CMD & hit command grunt exec:theme for first time after deploy the theme
Then grunt less:theme every time if we do any changes in less
If you are doing frequently changes in less use grunt watch so it will automatic hit grunt less:theme
Note : You might get Symlink error so go to file
.../app/etc/di.xmland comment out or remove code (below code ) in you local till development done, don't push chnaged code of this file to server, it only required in local if need.
<item name="view_preprocessed" xsi:type="object">Magento\Framework\App\View\Asset\MaterializationStrategy\Symlink</item>
Ok so based on what I have read in various other posts and my personal experience here is the process to regenerate the CSS or any Static Content:
php bin/magento setup:upgrade
This will wipe out all the necessary cache/generation/etc files without deleting anything you actually need.
php bin/magento setup:di:compile
This will then re-compile the files. Lastly:
php bin/magento setup:static-content:deploy
After the last command everything should be new and fresh, just refresh your page.
----- Notes ------
1) Make sure your file permissions and owner:user group is set correctly before doing this, you may need to speak about these with your sysadmin.
2) If your setup:upgrade command fails it might delete the files and be unable to write the new ones, as was my case due to file permissions. This can break your site, just iron out the permissions, then re-run the command.
3) I have not found (so far) a way to only re-compile a specific theme or file (would be super helpful if anyone finds a way) without writing a completely custom script
If anyone knows of something better let me know, because 12 minutes of compiling files because I had to make some css changes is extremely poor design in my eyes.
--- Edit ---
Magento 2 has Grunt tied into it and this makes refreshing CSS/LESS files infinitely easier if you take the time to configure it. Most the files are there as .sample files just remove the .sample file extension, configure the themes.js file in /dev/tools/grunt/configs folder. Then when you need to refresh your LESS files you simply run grunt less:--theme-- from the command line.
Resource: http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/css-topics/css_debug.html
-
1Stop providing the php bin/magento setup:static-content:deploy method, in developer mode magento has symlinksVlad Patru– Vlad Patru2017年04月19日 15:54:57 +00:00Commented Apr 19, 2017 at 15:54
-
Oh and manually deleting the folders, removing the .htaccess files and others unrelated, and forcing it to rebuild is the better method??? I added a comment about using Grunt, because simply moving your store to developer mode doesn't instantiate all the symlinks and refresh your css on page reload.Vallier– Vallier2017年04月19日 23:14:23 +00:00Commented Apr 19, 2017 at 23:14
do not forget to run the grunt exec:theme, grunt less:theme commands where theme is the theme you declared in the themes.js file from the config folder of grunt ( all this is in the developer guide ) and when you have magento in developer mode you shouldn't run the static deploy command, Magento2 works with Symlinks
-
1"when you have magento in developer mode you shouldn't run the static deploy command" <- This was extremely helpful to me. Thanks! I just deleted the contents of the pub/static directory (except for .htaccess of course), refreshed the page and symliks to my css files were automatically generated. Now I can edit them on the fly. Who said Magento 2 is not dev friendly?! Just need to know how to use it I guess.Rooster242– Rooster2422017年08月02日 21:37:05 +00:00Commented Aug 2, 2017 at 21:37
-
1Hi thank you mate, grunt less:default solved the problemYusuf Ibrahim– Yusuf Ibrahim2019年05月03日 02:38:29 +00:00Commented May 3, 2019 at 2:38
Run these commands on your Magento root folder:
alias mage="php -d memory_limit=-1 -f bin/magento"
chmod -R 775 pub/static/ var/ pub/media/ &&
rm -rf var/view_preprocessed/ var/cache/ var/page_cache/ var/tmp/ var/generation/ pub/static/frontend/ ;
mage cache:flush &
mage indexer:reindex &
mage setup:upgrade &&
mage setup:static-content:deploy ;
mage setup:db-data:upgrade &&
mage dev:source-theme:deploy &&
chmod -R 775 pub/static/ pub/media/ var/
-
Never thought of running a reindex in background 👍🏻Sergei Filippov– Sergei Filippov2017年07月12日 11:56:52 +00:00Commented Jul 12, 2017 at 11:56
-
Is it me or are these Magento 1 commands in answer to a Magento 2 question?Barry– Barry2019年04月26日 13:50:54 +00:00Commented Apr 26, 2019 at 13:50
-
There are no Magento 1 commands, they are from Magento 2Rafael Corrêa Gomes– Rafael Corrêa Gomes2019年04月30日 12:58:30 +00:00Commented Apr 30, 2019 at 12:58
Remove folder that contain your theme in pub/static/frontend
clear cache
You can use grunt to watch apply stylesheets. Read here
-
why regenerate with different version??? I have an issue with deploy file version not match. magento.stackexchange.com/questions/94502/…Magento2 Devloper– Magento2 Devloper2017年02月20日 08:15:33 +00:00Commented Feb 20, 2017 at 8:15
- Remove pub static.
- Give
pub/static777 permission - Clear cache.
- Run:
php bin/magento setup:static-content:deploy
You will see new css.
NOTE:
Give proper folder and file permission.
I hope this will help you.
Couldn't you just flush static cache in the admin panel and then
run php bin/magento setup:static-content:deploy
Make sure you are using developer mode.
Then execute following code:
chmod -R 777 var generated pub/static
grunt exec:default
grunt less:your_theme_name
Refresh the frontend page, These steps solved my problem.
-
i am in production mode. can you help me regarding this ?Sarvesh Tiwari– Sarvesh Tiwari2020年04月04日 16:02:50 +00:00Commented Apr 4, 2020 at 16:02
However, you may add the -f or --force flag to the setup command.
Example:
php bin/magento setup:static-content:deploy -f
I'm gonna drop my wisdom droplet here.
I use a cache_fly.sh bash script which basically does:
#!/bin/bash
start=`date +%s%N`
./magento setup:upgrade && \
./magento cache:flush && \
./magento setup:static-content:deploy -f
end=`date +%s%N`
runtime=$(bc <<< "scale=9;($end-$start)/1000000000")
echo "" && echo "" && echo -e "\e[34mTOTAL EXECUTION TIME: \e[1m\e[93m$runtime\e[0m"
NOTE: I've added an echo to show me how many time of my life I've wasted waiting for it to run.
Also ./magento is a script to do php bin/magento or call the same thing inside the docker container, if is docker used you could make yours or point to php bin/magento but if you're reading this you should know what I'm talking about, if not, please run asking for help.
Little adaptation for my case
#!/bin/bash
start=`date +%s%N`
chmod -R 775 pub/static/ var/ pub/media/ && \
rm -rf var/view_preprocessed/* var/cache/* var/page_cache/* var/tmp/* var/generation/* pub/static/frontend/* && \
./magento cache:flush && \
./magento setup:upgrade && \
./magento cache:clean && \
./magento s:s:d es_ES en_US -f && \
./magento setup:di:compile && \
chmod -R 775 pub/static/ pub/media/ var/
end=`date +%s%N
runtime=$(bc <<< "scale=9;($end-$start)/1000000000")
echo "" && echo "" && echo -e "\e[34mTOTAL EXECUTION TIME: \e[1m\e[93m$runtime\e[0m"
in my case it was all folder permession , the easy fix is to just do the following:
1- change the /pub folder permission to 777:
sudo chmod 777 pub -Rf
2- redeploy the site static contents:
bin/magento setup:static-content:deploy -f
in case u have multi-lang site, write like this:
bin/magento setup:static-content:deploy -f en_US ar_SA
This is a Very Simple Way.
cd pub/static
find . -depth -name .htaccess -prune -o -delete
cd ../..
bin/magento setup:static-content:deploy
Explore related questions
See similar questions with these tags.
.htaccessfile in thepub/staticfolder so it knows how to symlink files, if you deleted the pub/static folder that.htaccessis missing now