26

I have been working with Magento 2 for 2 months now. I realized that I am using bin/magento setup:upgrade way too much. I think I do not know when I have to run that command.

Here are the situations that I run the aforementioned command.

  • When I create a new Module,
  • When I add something to Setup directory,
  • When I edit etc/module.xml,
  • When I edit etc/di.xml,
  • When I edit etc/webapi.xml,
  • When I edit etc/adminhtml/system.xml

Question: In which situations is it absolutely necessary to run bin/magento setup:upgrade command?

(The reason I am asking is that it takes some time finish that command. I believe that it can really improve the productivity if I stop using it unnecessarily.)

Prince Patel
23.1k10 gold badges102 silver badges124 bronze badges
asked Jul 21, 2017 at 18:08

6 Answers 6

57

You only need to run setup:upgrade command

1. When you made changes in Setup script(InstallData, InstallSchema,
UpgradeData, UpgradeSchema, ...)

2. If you install Magento first time.

3. At the time of new module installation

4. After upgrade magento version.

What will do setup:upgrade command

1) Check module version in setup_module table

2) If version not available or new version added in module.xml, It will run setup script and add latest version number in table

If you made changes in HTML, CSS, JS, ... files you need to delete particular changed files from pub/static folder or run this command

php bin/magento setup:static-content:deploy

Short Form: php bin/magento s:s:d

If you made changes like add new dependency in __construct() or changes in di.xml, you need to delete changed files from var/generation folder or run this command

php bin/magento setup:di:compile

Short Form: php bin/magento s:d:c

If you made changes in admin configuration, layout xml, ui component, phtml, ... files you need to only clean or flush cache

php bin/magento cache:flush

Short Form: php bin/magento c:f

EDIT:

You can run cache flush by only typing c:f without using the full command php bin/magento c:f

Marius
199k55 gold badges431 silver badges837 bronze badges
answered Jul 21, 2017 at 19:07
0
3

1.When I create a new Module:

2.When I add something to Setup directory

3.When I edit etc/module.xml

php bin/magento module:enable
php bin/magento setup:upgrade

4.When I edit etc/module.xml,

5.When I edit etc/di.xml,

6.When I edit etc/webapi.xml,

7.When I edit etc/adminhtml/system.xml for xml changes cache flush is more than enough:

php bin/magento cache:flush

css and js changes: -- flush cache and php bin/magento setup:static-content:deploy

answered Dec 29, 2017 at 3:09
2
  • As far as I understand, setup:upgrade can be executed as an alternative to module:enable. Commented Jul 29, 2018 at 17:20
  • Are any scripts available combining these commands in 1? Commented Apr 15, 2019 at 11:12
2

If you work with developer mode you don't need to compile when you modify xml, you can make only a setup:upgrade.

And for others modifications as css, phtml, js, only flush cache, you needn't to deploy static content.

Manoj Deswal
5,80325 gold badges29 silver badges50 bronze badges
answered Jul 23, 2017 at 15:23
1
  • No, We don't need to run setup:upgrade for xml modification. For more details refer upper answer :) Commented Jul 24, 2017 at 17:22
2

I have created a bash file that could automate the process:

in your magento folder type 'vi magento_bash' copy and paste the code below. save file by escaping and :wq and enter hope this helps. Note: the below file takes only one command at a time.

#!/bin/sh
mainmenu()
{
echo 'Press 1 if you have created a new module'
echo 'Press 2 if you have changed HTML, CSS, JS..'
echo 'Press 3 if you have made changes like add new dependency in __construct() or changes in di.xml'
echo 'Press 4 if you have made changes like admin configuration, layout xml, ui component, phtml'
read -n 1 -p "Input Selection:" mainmenuinput
if [ "$mainmenuinput" = "1" ];
then
 cd src
 php bin/magento setup:upgrade
elif [ "$mainmenuinput" = "2" ];
then
 cd src
 php bin/magento s:s:d en_AU en_US
elif [ "$mainmenuinput" = "3" ];
then
 cd src
 php bin/magento setup:di:compile
elif [ "$mainmenuinput" = "4" ];
then
 cd src
 php bin/magento cache:flush
else
 echo 'You have selected an invalid selection'
fi
}
mainmenu
answered Feb 27, 2018 at 1:37
1
  • 1
    en_AU en_US behind ssd command? Commented Apr 15, 2019 at 11:13
1

Simply need to run bin/magento setup:upgrade whenever you do modification on the database.

For instance if you are creating attributes (programically), new modules, version upgrades etc.

So the basic rule to keep in mind is, if you know your code would do modifications on the database, then run the setup:upgrade command.

answered Feb 26, 2018 at 4:11
1
0

When you are deploying your code manually. All the above suggested methods are good enough to save time.

If you have auto deployment through Jenkins or some other tools, you should run all the commands as the tool don't know what you did and which file was changed.

answered Feb 27, 2018 at 2:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.