Magento CLI Commands — bin/magento Complete Guide
In this tutorial, you'll learn how to use the Magento CLI (bin/magento) to manage caches, run indexers, deploy static content, control modules, and create custom CLI commands for your own modules.
What You'll Learn
- How to manage all cache types from the command line
- How to run and configure indexers in different modes
- How to use setup commands for installation and compilation
- How to create custom CLI commands in your PHP modules
- How to manage admin users and configuration values
Why It Matters
The command line is the fastest way to manage a Magento installation. Every production deployment runs CLI commands. If you cannot use bin/magento effectively, you will struggle with deployments, troubleshooting, and automation. Mastering these commands makes you faster and more efficient.
Real-World Use
A deployment engineer needs to push code changes to production. The pipeline runs bin/magento maintenance:enable, then setup:upgrade, setup:di:compile, setup:static-content:deploy, and finally maintenance:disable — all in sequence. Without these commands, deployment would require manual clicks through the admin panel.
Learning Path
flowchart LR
A[REST API & GraphQL] --> B[CLI Commands]
B --> C[Caching]
B --> D[Indexing]
C --> E[Performance Optimization]
D --> E
style B fill:#3b82f6,color:#fff
Cache Commands
The Magento cache system has multiple types, each storing different data. The CLI gives you full control over each cache type.
Check Cache Status
bin/magento cache:status
Output shows each cache type and whether it is enabled or disabled:
Current status:
config: 1
layout: 1
block_html: 1
collections: 1
reflection: 1
db_ddl: 1
eav: 1
config_api: 1
full_page: 1
translate: 1
config_integration: 1
config_integration_api: 1
target_rule: 1
Clean and Flush Cache
cache:clean removes only Magento's cache storage, not other applications' cache. cache:flush clears all cache storage including file-based cache.
# Clean specific cache types
bin/magento cache:clean config layout block_html
# Clean all cache types
bin/magento cache:clean
# Flush all cache storage
bin/magento cache:flush
Enable and Disable Cache
# Enable all cache types
bin/magento cache:enable
# Disable specific cache types
bin/magento cache:disable block_html full_page
# Enable specific cache types
bin/magento cache:enable full_page
Indexer Commands
Indexers transform data for fast storefront access. Without indexing, product searches, category pages, and price calculations would be slow.
List and Check Indexers
# List all indexers
bin/magento indexer:info
# Check indexer status
bin/magento indexer:status
Reindex
# Reindex all
bin/magento indexer:reindex
# Reindex specific type
bin/magento indexer:reindex catalog_product_price catalog_category_product
Set Indexer Mode
Indexers can run in two modes: Update on Save (immediate) or Update by Schedule (cron-based).
# Show current modes
bin/magento indexer:show-mode
# Set to schedule mode (recommended for production)
bin/magento indexer:set-mode schedule
# Set to real-time mode
bin/magento indexer:set-mode real_time
Setup Commands
Setup commands handle installation, upgrades, compilation, and static content.
Upgrade and Compile
# Run database schema and data updates
bin/magento setup:upgrade
# Generate dependency injection configuration
bin/magento setup:di:compile
# Deploy static view files
bin/magento setup:static-content:deploy -f
# Deploy for specific languages
bin/magento setup:static-content:deploy en_US de_DE fr_FR -f
Store Configuration
# Set store configuration from CLI
bin/magento setup:store-config:set --base-url="https://mystore.com"
Uninstall
# Remove Magento database and configuration
bin/magento setup:uninstall
Deploy Mode
Magento has three modes: developer, default, and production. Each affects error reporting and performance.
# Switch to developer mode
bin/magento deploy:mode:set developer
# Switch to production mode
bin/magento deploy:mode:set production
# Show current mode
bin/magento deploy:mode:show
In developer mode, static content is generated on demand and errors show full stack traces. In production mode, static content must be pre-deployed and errors show generic messages.
Module Commands
You can enable, disable, and check modules without touching the admin panel.
# List all modules with status
bin/magento module:status
# Enable modules
bin/magento module:enable MyCompany_MyModule
# Disable modules
bin/magento module:disable MyCompany_MyModule
# Uninstall module
bin/magento module:uninstall MyCompany_MyModule
Admin User Commands
Create and manage admin users from the command line during deployment.
# Create admin user
bin/magento admin:user:create \
--admin-user="admin" \
--admin-password="SecurePass123!" \
--admin-email="admin@example.com" \
--admin-firstname="Admin" \
--admin-lastname="User"
# Unlock locked admin account
bin/magento admin:user:unlock admin
Config Commands
Read and write configuration values directly without navigating the admin panel.
# Read config value
bin/magento config:show web/unsecure/base_url
# Set config value
bin/magento config:set web/unsecure/base_url https://mystore.com
# Set sensitive config (encrypted)
bin/magento config:sensitive:set payment/braintree/merchant_id "your_id"
Maintenance Commands
Maintenance mode prevents customers from accessing the store during updates.
# Enable maintenance mode
bin/magento maintenance:enable
# Disable maintenance mode
bin/magento maintenance:disable
# Allow specific IPs through maintenance mode
bin/magento maintenance:allow-ips 192.168.1.10 10.0.0.5
# Show maintenance mode status
bin/magento maintenance:status
Custom CLI Command Development
You can create custom CLI commands in your own modules using Symfony Console components.
Step 1: Create the Command Class
Create app/code/MyCompany/MyModule/Console/Command/WelcomeCommand.php:
<?php
namespace MyCompany\MyModule\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
class WelcomeCommand extends Command
{
protected function configure()
{
$this->setName('mycompany:welcome')
->setDescription('Display a welcome message')
->addArgument('name', InputArgument::REQUIRED, 'Your name');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
$output->writeln("Hello, $name! Welcome to MyModule CLI.");
return Command::SUCCESS;
}
}
Step 2: Register in di.xml
Add to app/code/MyCompany/MyModule/etc/di.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="mycompany_welcome" xsi:type="object">MyCompany\MyModule\Console\Command\WelcomeCommand</item>
</argument>
</arguments>
</type>
</config>
Step 3: Run Your Command
bin/magento mycompany:welcome "Developer"
Output:
Hello, Developer! Welcome to MyModule CLI.
Common Mistakes
- Running
setup:upgradewithout maintenance mode in production, causing database errors when customers access the site during schema changes - Deploying static content in developer mode and wondering why the production site looks broken, because developer mode generates it on the fly
- Forgetting to run
cache:flushafter changing configuration, leaving old cached values active - Reindexing all indexers on a large catalog during business hours, causing high server load and slow response times
- Running
setup:di:compilebeforesetup:upgrade, which compiles against the old database schema and causes errors
Practice Questions
- What is the difference between
cache:cleanandcache:flush? - Why should you set indexers to
Update by Schedulemode in production? - What commands would you run for a full production deployment after code changes?
Challenge: Create a custom CLI command that exports all products with low stock to a CSV file and saves it to the var/export directory.
FAQ
Mini Project
Create a CLI command called mycompany:sync-products that reads product data from a CSV file, validates the SKUs exist in Magento, updates prices and stock quantities, and logs all changes to var/log/sync.log. Use the product Repository interface to update products programmatically.
What's Next
Now that you can manage Magento from the command line, learn about Magento Caching to understand how Varnish and Redis improve store performance. Then continue with Magento Indexing to optimize data processing for large catalogs.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro