25

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

6 Answers 6

56

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

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

2
  • As far as I understand, setup:upgrade can be executed as an alternative to module:enable.
    – Muckee
    Jul 29, 2018 at 17:20
  • Are any scripts available combining these commands in 1?
    – snh_nl
    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.

1
  • No, We don't need to run setup:upgrade for xml modification. For more details refer upper answer :) 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
1
  • 1
    en_AU en_US behind ssd command?
    – snh_nl
    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.

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.

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.