Magento 2 CLI Commands: Essential Reference for Developers
The Magento 2 CLI (bin/magento) is the primary tool for deployment, maintenance, and troubleshooting. Running commands in the wrong order or skipping steps causes some of the most common issues reported by store administrators and developers.
Deployment Sequence
When deploying code changes or after installing/updating extensions, run the commands in this exact order. Skipping steps or changing the order leads to broken DI compilation, missing static assets, or outdated database schemas.
# 1. Update database schemas and data
php bin/magento setup:upgrade
# 2. Flush all cache (required before DI compile)
php bin/magento cache:flush
# 3. Compile dependency injection configuration
php bin/magento setup:di:compile
# 4. Deploy static content (CSS, JS, images, templates)
php bin/magento setup:static-content:deploy
# 5. Clean cache to activate new content
php bin/magento cache:clean
Why This Order Matters
setup:upgrademust run beforesetup:di:compile: DI compilation reads the current database state to resolve module dependencies. Running DI compile on an outdated database schema produces incorrect generated code.cache:flushbetween upgrade and compile: Stale cache from the previous deployment can interfere with DI compilation.setup:static-content:deployafter DI compile: Static content deployment uses the compiled DI configuration to resolve template paths and block classes.
Cache Management
cache:clean vs cache:flush
These two commands behave differently:
cache:clean: Removes only invalidated cache entries (entries tagged as outdated). Other cache entries remain intact. This is the safer, incremental option.cache:flush: Empties the entire cache storage, regardless of invalidation status. This is a full reset.
Use cache:clean for routine operations. Use cache:flush when you need a guaranteed clean slate (e.g., after deployment or when troubleshooting unexplained behavior).
Clearing Static Content
When static content changes are not reflected on the frontend after deployment, manually clear the static content directories:
rm -rf pub/static/* var/view_preprocessed/*
Then redeploy:
php bin/magento setup:static-content:deploy
Selective Cache Operations
Enable or disable specific cache types:
# List all cache types and their status
php bin/magento cache:status
# Disable specific cache type
php bin/magento cache:disable full_page
# Enable specific cache type
php bin/magento cache:enable full_page
# Clean specific cache type only
php bin/magento cache:clean full_page config
Module Management
Listing Modules
# List all modules and their status
php bin/magento module:status
# List only enabled modules
php bin/magento module:status --enabled
# List only disabled modules
php bin/magento module:status --disabled
Enabling and Disabling Modules
# Enable a module
php bin/magento module:enable Vendor_Module
# Disable a module
php bin/magento module:disable Vendor_Module
After enabling or disabling modules, run the full deployment sequence starting from setup:upgrade.
Removing a Broken Module from the Database
If setup:upgrade fails because a module was removed from the filesystem but its database entry remains, delete it from the setup_module table:
DELETE FROM setup_module WHERE module = 'Vendor_ModuleName';
Then run setup:upgrade again. This situation commonly occurs when an extension is removed by deleting files instead of using composer remove.
Indexer Commands
# Show all indexers and their status
php bin/magento indexer:status
# Reindex all indexers
php bin/magento indexer:reindex
# Reindex a specific indexer
php bin/magento indexer:reindex catalog_product_price
# Set indexer mode
php bin/magento indexer:set-mode schedule # Update on Schedule
php bin/magento indexer:set-mode realtime # Update on Save
# Reset indexer state (useful for stuck indexers)
php bin/magento indexer:reset
See also: How to Reindex in Magento 2
Static Content Deployment
# Deploy for all locales
php bin/magento setup:static-content:deploy
# Deploy for specific locales
php bin/magento setup:static-content:deploy en_US de_DE fr_FR
# Force deploy in developer mode
php bin/magento setup:static-content:deploy -f
# Deploy specific themes only
php bin/magento setup:static-content:deploy --theme Vendor/theme
Maintenance Mode
# Enable maintenance mode
php bin/magento maintenance:enable
# Disable maintenance mode
php bin/magento maintenance:disable
# Allow specific IPs during maintenance
php bin/magento maintenance:allow-ips 192.168.1.100
See also: Magento 2 Maintenance Mode
Troubleshooting CLI Issues
MySQL Connection Errors During Long Commands
Long-running CLI operations (large imports, full reindex) may fail with MySQL "server has gone away" errors. This happens when the MySQL connection timeout is shorter than the command execution time.
Solutions:
- Increase
wait_timeoutandinteractive_timeoutin MySQL configuration - Check if hosting provider limits connection duration
- For very long operations, split them into smaller batches
Memory Exhaustion
# Increase memory limit for a single command
php -d memory_limit=4G bin/magento setup:di:compile
Permission Errors After CLI Commands
CLI commands run as the current shell user, which may differ from the web server user. After running CLI commands, fix ownership:
# Adjust to your web server user (www-data, apache, nginx)
chown -R www-data:www-data var/ generated/ pub/static/
See also: Magento 2 Correct Permissions