Back to Top

Useful Commands in Magento 2

Updated 24 September 2024

In Magento 2, commands are used to perform various administrative tasks, manage the application, and facilitate development.

These commands are typically run from the command line interface (CLI) using the bin/magento script.

Magento 2 command :

In Magento 2, a variety of commands are available to serve different functions.

We run all these commands from the Magento 2 root directory (The directory where Magento 2 is installed).

Open the Magento 2 root directory in terminal :

You can open the Magento 2 root directory in the terminal by simply opening the Magento 2 root directory folder in the file explorer.

Searching for an experienced
Magento 2 Company ?
Find out More
Selection_104

Now right-click on the mouse and select the “Open in Terminal” option.

Screenshot-from-2023-07-11-19-01-07

It will open your Magento 2 root directory in the terminal where you can execute your Magento 2 commands.

Selection_105
Magento 2 root directory in terminal

Here you can execute your Magento 2 commands.

Execute Magento 2 command

Magento 2 basic commands :

  • php bin/magento deploy:mode:show
  • php bin/magento setup:upgrade
  • php bin/magento setup:di:compile
  • php bin/magento setup:static-content:deploy
  • php bin/magento indexer:reindex
  • php bin/magento cache:flush

deploy:mode:show

This command uses to check the current execution mode of Magento 2.

setup:upgrade

This command uses to install the new modules in Magento 2. It checks for any pending schema or data changes from installed or updated modules.

setup:di:compile

This command uses to compile code like Factories, Proxies, Interceptors, etc., and puts them in generated or var/generation directory.

Here are a couple of things that will happen when you run the setup:di:compile command:

  • Area Configuration Aggregation – It will optimize the dependency injection configurations according to the area.
  • Application Code Generation – for Proxies, Factories, Plugins, etc.
  • Interceptor Generation – It will optimize the code generation of interceptors
  • Interceptor Cache Generation – It will optimize the cache generation of interceptors.
  • Service Data Attributes Generation – It will generate extension classes for the data objects.
  • Repositories Code Generation – It will simply generate the necessary code for APIs.

setup:static-content:deploy

This command helps to write static files to the Magento 2 file system. It uses to optimize static files properly, allowing for a better user experience.

NOTE: In “default” and “developer” modes, the system does not necessitate manual static content deployment.

In developer mode, the system automatically deploys static contents on demand without the need for manual static content deployment.

If you still want to deploy in these modes, use -f option: ‘bin/magento setup:static-content:deploy -f’

indexer: reindex

The Mangeto indexer: reindex command uses to update the transformed data as the data changes.

Ex : After updating a product price, you need to reindex the data for it to be correctly displayed on the frontend.

cache:flush

This command uses to remove all stored data from the cache, so it may affect the performance of other processes using the same storage.

Conclusion

Magento 2 commands are essential for managing and optimizing the platform, enabling users to perform a wide range of tasks efficiently through the command line interface (CLI).

By executing commands such as
setup:upgrade
setup:di:compile
setup:static-content:deploy
indexer:reindex
and cache:flush

users can effectively configure their application, optimize performance, and ensure that changes are reflected on the frontend.

Mastering these commands is vital for anyone looking to enhance their Magento 2 experience, streamline operations, and maintain an efficient e-commerce environment.

Ultimately, leveraging these CLI commands empowers users to take full control of their Magento 2 installations and drive successful online commerce.

Explore more about Magento 2 commands.

Continue Reading -> Magento 2 Area codes

Previous Blog -> Magento 2 Architecture

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home