50

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?

  • 8
    FYI: Magento 2 has a developer mode so you don't have to deploy static content all the time. I've wasted hours and hours in the past month redeploying static content. So don't be like me, and switch to developer mode (on your local machine. Your live server should be on production mode) – Nathan Merrill Dec 22 '16 at 14:40
  • why regenerate with different version??? I have an issue with deploy file version not match. magento.stackexchange.com/questions/94502/… – Magento2 Devloper Feb 20 '17 at 8:15
  • 3
    If magento is in developer mode it will create symlinks to the static files but for this to happen you have to have .htaccess file in the pub/static folder so it knows how to symlink files, if you deleted the pub/static folder that .htaccess is missing now – Vlad Patru Mar 30 '17 at 7:16
  • @NathanMerrill you are so right but most of the dev's don't read documentation and all the answers and still run the static-content:deploy command and what is a bit sad is that some of the companies with account on Stackexchange provide answers like that, waste of time – Vlad Patru Apr 19 '17 at 15:53

15 Answers 15

67

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!

  1. Remove pub/static [Keep the .htaccess backed up and copy it again]
    1. Remove var/cache
    2. Remove var/composer_home
    3. Remove var/generation
    4. Remove var/page_cache
    5. Remove var/view_preprocessed
    6. run php bin/magento setup:static-content:deploy
  • 48
    M2 doesn't make things very developer friendly. – Matthew McLennan Feb 19 '16 at 18:43
  • 10
    @CarComp no files should ever have to be deleted manually. magento should take care of that. it is very annoying – Claudiu Creanga Jun 3 '16 at 10:35
  • 10
    U must be new to magento ;) – CarComp Jun 3 '16 at 11:46
  • 6
    I 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 preserved – Elio Ermini Sep 8 '16 at 9:42
  • 3
    I am new to "magento". WAT? Is it the official way to update css? – puchu Mar 1 '17 at 10:08
20

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

find . -depth -name .htaccess -prune -o -delete

then you can run

rm -rf var/cache/ var/generation/ var/page_cache/ var/view_preprocessed/ 
10

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.

9

As the question is for js and css update in Magento 2 in developer mode.

  1. 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.
  2. Updating the CSS in your respected module, do the pt 1.
  3. 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.xml and 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>
7

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

  1. When you create new file, rather than modifying an existing file, you may also need to clear the cache, php bin/magento cache:flush.

  2. 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)

  3. In production mode, you may also need php bin/magento setup:static-content:deploy and php bin/magento cache:flush, I don't try production mode yet.

6

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

  • Stop providing the php bin/magento setup:static-content:deploy method, in developer mode magento has symlinks – Vlad Patru Apr 19 '17 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 Apr 19 '17 at 23:14
6

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 Aug 2 '17 at 21:37
  • 1
    Hi thank you mate, grunt less:default solved the problem – Yusuf Ibrahim May 3 at 2:38
6

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 Jul 12 '17 at 11:56
  • Is it me or are these Magento 1 commands in answer to a Magento 2 question? – Barry Apr 26 at 13:50
  • There are no Magento 1 commands, they are from Magento 2 – Rafael Corrêa Gomes Apr 30 at 12:58
6

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:

  1. Remove the files from the pub/static folder: rm -rf pub/static/*

  2. Remove the files from the var/view_preprocessed folder: rm -rf var/view_preprocessed/*

  3. Recompile the pub static folder: php bin/magento setup:static-content:deploy

  4. Clear cache is it's turned on: php bin/magento cache:clean

  5. Refresh the browser.

  • 1
    would be nice if magento2 would make symlinks to those affected files in developer mode, then changes in those module files where visible on the fly – roman204 Jan 25 '18 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 Jan 25 '18 at 14:04
5

Remove folder that contain your theme in pub/static/frontend
clear cache
You can use grunt to watch apply stylesheets. Read here

5
  1. Remove pub static.
  2. Give pub/static 777 permission
  3. Clear cache.
  4. 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.

  • "You will see new css." - I didn't but thank you – Barry Apr 26 at 14:16
4

Couldn't you just flush static cache in the admin panel and then

run php bin/magento setup:static-content:deploy
1

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.

  • nice, it really help me thanks – Yoesoff May 29 at 9:14
0

However, you may add the -f or --force flag to the setup command.

Example:

php bin/magento setup:static-content:deploy -f
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.