59

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?

6
  • 9
    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) Commented Dec 22, 2016 at 14:40
  • why regenerate with different version??? I have an issue with deploy file version not match. magento.stackexchange.com/questions/94502/… Commented Feb 20, 2017 at 8:15
  • 4
    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
    Commented Mar 30, 2017 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
    Commented Apr 19, 2017 at 15:53
  • I get annoyed every time I see those comments. You should not have to go to developer mode just to deploy small css changes.
    – iphigenie
    Commented Jan 12, 2020 at 12:23

18 Answers 18

81

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)
  2. Remove var/cache
  3. Remove var/composer_home
  4. Remove var/generation
  5. Remove var/page_cache
  6. Remove var/view_preprocessed
  7. Run php bin/magento setup:static-content:deploy
10
  • 67
    M2 doesn't make things very developer friendly. Commented 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 annoying Commented Jun 3, 2016 at 10:35
  • 14
    U must be new to magento ;)
    – CarComp
    Commented Jun 3, 2016 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 Commented Sep 8, 2016 at 9:42
  • 3
    I am new to "magento". WAT? Is it the official way to update css?
    – puchu
    Commented Mar 1, 2017 at 10:08
25

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!

7
  • why regenerate with different version??? I have an issue with deploy file version not match. magento.stackexchange.com/questions/94502/… Commented Feb 20, 2017 at 8:15
  • 1
    did not work as expectedd Commented May 9, 2017 at 11:28
  • 2
    this command delete all of my directories -_- Commented Feb 5, 2020 at 13:45
  • @IzharMehdi did you enter the pub/static directory like it said in the answer? Commented Feb 5, 2020 at 14:51
  • 2
    I deleted all files in prod website, didn't enter pub/static directory please add command cd pub/static at the beginning Commented Oct 30, 2020 at 16:57
11

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.

1
10

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 need deploy static files and clean cache by running bin/magento setup:static-content:deploy and bin/magento cache:flush.

1
  • it's not working for me Commented Apr 4, 2020 at 15:52
9

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.

2
  • 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
    Commented 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
    Commented Jan 25, 2018 at 14:04
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

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

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

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

2
  • 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
    Commented Aug 2, 2017 at 21:37
  • 1
    Hi thank you mate, grunt less:default solved the problem Commented May 3, 2019 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/
3
  • Never thought of running a reindex in background 👍🏻 Commented Jul 12, 2017 at 11:56
  • Is it me or are these Magento 1 commands in answer to a Magento 2 question?
    – Barry
    Commented Apr 26, 2019 at 13:50
  • There are no Magento 1 commands, they are from Magento 2 Commented Apr 30, 2019 at 12:58
5

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

1
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.

1
  • "You will see new css." - I didn't but thank you
    – Barry
    Commented Apr 26, 2019 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.

2
  • nice, it really help me thanks
    – Yoesoff
    Commented May 29, 2019 at 9:14
  • i am in production mode. can you help me regarding this ? Commented Apr 4, 2020 at 16:02
0

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

Example:

php bin/magento setup:static-content:deploy -f
0
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.

0

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"
0

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
0

This is a Very Simple Way.

cd pub/static
find . -depth -name .htaccess -prune -o -delete
cd ../..
bin/magento setup:static-content:deploy

Please read the latest Magento document!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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