Useful commands for Magento 2 development

If you are a developer and working in Magento 2 you should surely know about the various commands which is used in Magento 2. Commands are very useful for the developer and it perform many task in less time, task performed through the commands are relatively very fast than doing it manually. In this post I am trying to describe the different commands, steps to run the commands and uses, please see below:
1. php bin/magento setup:di:compile
2. chmod 777 var/ -R; chmod 777 generated/ -R; chmod 777 pub/static -R
3. php -dmemory_limit=6G bin/magento setup:upgrade
4. chmod 777 var/ -R; chmod 777 generated/ -R; chmod 777 pub/static -R
5. php -dmemory_limit=6G bin/magento cache:flush
6. php -dmemory_limit=6G bin/magento setup:static-content:deploy -f
7. php -dmemory_limit=6G bin/magento index:reindex
8. php -dmemory_limit=6G bin/magento cache:flush

See the Description below for each commands:


php bin/magento setup:di:compile Or php -dmemory_limit=6G bin/magento setup:di:compile

If you have done any code change specially with factory methods ,proxy, add plugins or any code compilation then you must need to run the di:compile command.

setup:di:compile command generates the contents of the var/di folder in Magento <2.2 and generated for Magento >= 2.2

According to Magento, this serves the following purpose:
– Area configuration aggregation (that is, optimized dependency injection configurations per area)
– Interceptor generation (that is, optimized code generation of interceptors)
– Interception cache generation
– Service data attributes generation (that is, generated extension classes for data objects)
– Application code generation (factories, proxies, and so on)
– Repositories code generation (that is, generated code for APIs)


chmod 777 var/ -R; chmod 777 generated/ -R; chmod 777 pub/static -R

Make sure after run the Compile command you should run this above command to give these folders full permission.


php bin/magento setup:upgrade Or php -dmemory_limit=6G bin/magento setup:upgrade

If you have installed a new extension or have made changes to the /setup/ directory of your module. i.e. added new column or changed something in the UpgradeSchema.php file then you need to run the php bin/magento setup:upgrade commands.

magento setup:upgrade command clears all the compiled code and the cache. If you do not wants to clear the code and cache you can run this commands:

php bin/magento setup:upgrade –keep-generated then it should be prevents generated code from being deleted.


php -dmemory_limit=6G bin/magento cache:flush

Make sure after run the Upgrade command you should run this above command to delete the cache.


php -dmemory_limit=6G bin/magento setup:static-content:deploy -f 

This is to deploy the Static content in Magento 2.


php -dmemory_limit=6G bin/magento index:reindex

This is for reindxing the data in Magento 2.


php -dmemory_limit=6G bin/magento cache:flush

Finally run the cache flush command.

I hope this will be surely helpful for the developer and you enjoyed the reading. Stay tuned for the Magento 2 latest tutorial and updates, Hope you enjoyed reading, if you need the professional Magento 2 Development / PHP we can help you, just Click on the Link and send me your requirements.