Magento 2 is a powerful and popular eCommerce platform that offers many features and benefits for online retailers. However, it also requires some technical skills and knowledge to set up, manage, and customize your online store. That’s where Magento 2 CLI (Command Line Interface) comes in handy.
Magento 2 CLI is a text-based interface that allows you to execute commands on your server to perform various tasks related to your Magento 2 store. You can use Magento 2 CLI to install, upgrade, configure, deploy, test, debug, and optimize your store without using the admin panel or a graphical user interface.
In this article, we will show you how to use Magento 2 CLI and its commands to make your Magento development easier and faster. We will also show you how to create your own custom commands for Magento 2 CLI. Let’s get started!
How to Use Magento 2 CLI?
To use Magento 2 CLI, you need to have SSH (Secure Shell) access to your server and navigate to your Magento root directory using a terminal program such as PuTTY or Terminal. You also need to make the CLI executable with this command:
chmod 744 bin/magento
To see the list of available commands and options for Magento 2 CLI, you can type this command:
bin/magento
You can also get help for a specific command by typing this command:
bin/magento help command
For example, if you want to get help for the cache:clean command, you can type this command:
bin/magento help cache:clean
Magento 2 CLI Commands List
Magento 2 CLI offers a variety of commands for different purposes and categories. Here are some of the most useful and common commands for Magento developers:
Admin Commands
These commands are related to the admin user management and configuration.
- admin:user:create – Creates an administrator user with the specified options.
- admin:user:unlock – Unlocks an admin user account that has been locked due to too many failed login attempts.
Cache Commands
These commands are related to the cache management and optimization.
- cache:clean – Cleans the cache types specified in the options or all cache types if none are specified.
- cache:disable – Disables the cache types specified in the options or all cache types if none are specified.
- cache:enable – Enables the cache types specified in the options or all cache types if none are specified.
- cache:flush – Flushes the cache storage used by the cache types specified in the options or all cache types if none are specified.
- cache:status – Checks the status of the cache types.
Catalog Commands
These commands are related to the catalog management and optimization.
- catalog:images:resize – Creates resized product images according to the image dimensions defined in the view.xml file.
- catalog:product:attributes:cleanup – Removes unused product attributes from the database.
Cron Commands
These commands are related to the cron job management and configuration.
- cron:install – Generates and installs the crontab for the current user.
- cron:remove – Removes the crontab for the current user.
- cron:run – Runs cron jobs by schedule.
Deploy Commands
These commands are related to the deployment mode and configuration.
- deploy:mode:set – Sets the application mode to one of these values: default, developer, or production.
- deploy:mode:show – Displays the current application mode.
Dev Commands
These commands are related to the development mode and configuration.
- dev:source-theme:deploy – Collects and publishes source files for a theme.
- dev:tests:run – Runs tests according to the specified options.
- dev:urn-catalog:generate – Generates the catalog of URNs (Uniform Resource Names) to *.xsd mappings for
- dev:urn-catalog:generate – Generates the catalog of URNs (Uniform Resource Names) to *.xsd mappings for the IDE (Integrated Development Environment) to highlight XML files.
- dev:xml:convert – Converts XML files using XSL (Extensible Stylesheet Language) style sheets.
I18n Commands
These commands are related to the internationalization and localization of your store.
- i18n:collect-phrases – Discovers phrases in the codebase and writes them to a CSV file.
- i18n:pack – Saves language packages according to the specified options.
- i18n:uninstall – Uninstalls language packages.
Indexer Commands
These commands are related to the indexer management and optimization.
- indexer:info – Shows the allowed indexers and their IDs.
- indexer:reindex – Reindexes data for the specified indexers or all indexers if none are specified.
- indexer:reset – Resets the status of the specified indexers or all indexers if none are specified to invalid.
- indexer:set-mode – Sets the index mode for the specified indexers or all indexers if none are specified to one of these values: realtime or schedule.
- indexer:show-mode – Shows the index mode for the specified indexers or all indexers if none are specified.
- indexer:status – Shows the status of the specified indexers or all indexers if none are specified.
Info Commands
These commands are related to the information and configuration of your store.
- info:adminuri – Displays the Magento admin URI.
- info:backups:list – Prints the list of available backup files.
- info:currency:list – Displays the list of available currency codes.
- info:dependencies:show-framework – Shows the number of dependencies on Magento framework components.
- info:dependencies:show-modules – Shows the number of dependencies between modules.
- info:dependencies:show-modules-circular – Shows the number of circular dependencies between modules.
- info:language:list – Displays the list of available language locales.
- info:timezone:list – Displays the list of available timezones.
Maintenance Commands
These commands are related to the maintenance mode and configuration.
- maintenance:allow-ips – Sets maintenance mode exempt IPs for the specified options or none if none are specified.
- maintenance:disable – Disables maintenance mode.
- maintenance:enable – Enables maintenance mode for the specified options or none if none are specified.
- maintenance:status – Displays maintenance mode status.
Module Commands
These commands are related to the module management and configuration.
- module:disable – Disables specified modules or all modules if none are specified.
- module:enable – Enables specified modules or all modules if none are specified.
- module:status – Displays status of modules or a specific module if one is specified.
- module:uninstall – Uninstalls modules installed by Composer.
Sampledata Commands
These commands are related to the sample data management and configuration.
- sampledata:deploy – Deploys sample data modules using Composer.
- sampledata:remove – Removes all sample data packages from composer.json file.
- sampledata:reset – Resets all sample data modules for re-installation.
Setup Commands
These commands are related to the setup and configuration of your store.
- setup:backup – Takes backup of Magento application codebase, media, and database according to the specified options.
- setup:config:set – Creates or modifies the deployment configuration according to the specified options.
- setup:cron:run – Runs cron job scheduled for setup application.
- setup:db-data:upgrade – Installs and upgrades data in the database according to your module’s data scripts.
- setup:db-schema:upgrade – Installs and upgrades the database schema according to your module’s schema scripts.
- setup:db:status – Checks if the database schema or data requires upgrade.
- setup:di:compile – Generates dependency injection configuration and all missing classes that can be auto-generated.
- setup:install – Installs the Magento application according to the specified options.
- setup:performance:generate-fixtures – Generates fixtures according to the specified profile.
- setup:rollback – Rolls back Magento application codebase, media, and database according to the specified options.
- setup:static-content:deploy – Deploys static view files according to the specified options.
- setup:store-config:set – Installs the store configuration according to the specified options.
- setup:uninstall – Uninstalls the Magento application.
- setup:upgrade – Upgrades the Magento application, database data, and schema.
Theme Commands
These commands are related to the theme management and configuration.
- theme:uninstall – Uninstalls themes.
How to Create Custom Commands for Magento 2 CLI?
Magento 2 CLI also allows you to create your own custom commands for your specific needs and purposes. You can create custom commands by following these steps:
- Create a module for your custom command. You need to create a module for your custom command and register it in the app/etc/config.php file. You can follow this guide to create a module in Magento 2.
- Create a command class for your custom command. You need to create a command class for your custom command and extend it from the Symfony\Component\Console\Command\Command class. You also need to implement the following methods in your command class:
- configure() – This method defines the name, description, arguments, and options of your custom command.
- execute() – This method contains the logic of your custom command and returns an exit code.
- initialize() (optional) – This method initializes variables before executing your custom command.
- interact() (optional) – This method interacts with the user before executing your custom command.
- Register your command class in the di.xml file. You need to register your command class in the etc/di.xml file of your module and add it to the Magento\Framework\Console\CommandList argument. For example:
<type name="Magento\Framework\Console\CommandList"> <arguments> <argument name="commands" xsi:type="array"> <item name="custom_command" xsi:type="object">Vendor\Module\Console\Command\CustomCommand</item> </argument> </arguments> </type>
- Run your custom command. You can run your custom command by typing this command:
bin/magento custom_command
You can also get help for your custom command by typing this command:
bin/magento help custom_command
Conclusion
Magento 2 CLI is a powerful tool that can help you perform various tasks related to your Magento 2 store with ease and efficiency. You can use Magento 2 CLI commands to install, upgrade, configure, deploy, test, debug, and optimize your store without using the admin panel or a graphical user interface. You can also create your own custom commands for Magento 2 CLI to suit your specific needs and purposes.
We hope this article has given you a handy guide on how to use Magento 2 CLI and its commands for Magento developers. If you have any questions or feedback, please feel free to leave a comment below.