Files
2025-11-30 08:45:19 +08:00

6.6 KiB

Drush Commands Reference

Comprehensive Drush command reference for Drupal 8-11+. These commands work with Drush 10, 11, and 12.

Cache Management

# Clear all caches
drush cache:rebuild
drush cr

# Clear specific cache bins
drush cache:clear css-js
drush cache:clear render

# List all cache bins
drush cache:bins

Configuration Management

# Export configuration
drush config:export
drush cex

# Import configuration
drush config:import
drush cim

# Get configuration value
drush config:get system.site name
drush cget system.site name

# Set configuration value
drush config:set system.site name "My Site"
drush cset system.site name "My Site"

# Edit configuration in editor
drush config:edit system.site

# Delete configuration
drush config:delete mymodule.settings

# List all configuration
drush config:list

Module Management

# Enable module
drush pm:enable mymodule
drush en mymodule

# Uninstall module
drush pm:uninstall mymodule
drush pmu mymodule

# List all modules
drush pm:list
drush pml

# List enabled modules only
drush pm:list --status=enabled

# Download module from drupal.org
drush pm:download webform
drush dl webform

Database Operations

# Update database (run update hooks)
drush updatedb
drush updb

# Execute SQL query
drush sql:query "SELECT * FROM users WHERE uid=1"
drush sqlq "SELECT * FROM users WHERE uid=1"

# Connect to database CLI
drush sql:cli
drush sqlc

# Dump database to file
drush sql:dump > backup.sql
drush sql:dump --gzip > backup.sql.gz

# Drop all tables
drush sql:drop

# Sanitize database (for dev environments)
drush sql:sanitize

User Management

# Login as user 1
drush user:login
drush uli

# Login as specific user
drush user:login admin
drush uli admin

# Create user
drush user:create newuser --mail="user@example.com" --password="password"

# Cancel/delete user
drush user:cancel username

# Add role to user
drush user:role:add "administrator" username

# Remove role from user
drush user:role:remove "administrator" username

# Change user password
drush user:password admin "newpassword"

# Block user
drush user:block username

# Unblock user
drush user:unblock username

Content Management

# Generate test content
drush devel:generate:content 50 --bundles=article

# Delete content
drush entity:delete node --bundle=article

# Generate users
drush devel:generate:users 20

State Management

# Get state value
drush state:get system.maintenance_mode

# Set state value
drush state:set system.maintenance_mode 1

# Delete state value
drush state:delete mymodule.last_run

Theme Management

# List all themes
drush theme:list

# Enable theme
drush theme:enable mytheme

# Uninstall theme
drush theme:uninstall oldtheme

# Set default theme
drush config:set system.theme default mytheme

Cron

# Run cron
drush cron

# Run cron for specific module
drush cron mymodule

Status & Information

# Show site status
drush status
drush st

# Show core status and available updates
drush pm:security

# Show Drupal version
drush core:status

# Show requirements report
drush core:requirements

Development & Debugging

# Watch for file changes and clear cache
drush watchdog:tail

# Show recent log messages
drush watchdog:show

# Show recent log messages and follow
drush watchdog:tail

# Evaluate PHP code
drush php:eval "echo \Drupal::VERSION;"
drush ev "echo \Drupal::VERSION;"

# Open PHP REPL
drush php:cli

Generate Code (Drush Generate)

# Generate module
drush generate module

# Generate controller
drush generate controller

# Generate form
drush generate form

# Generate plugin block
drush generate plugin:block

# Generate service
drush generate service

# Generate theme
drush generate theme

# Generate hook implementation
drush generate hook

# Generate event subscriber
drush generate event-subscriber

# List all generators
drush generate --help

Field Management

# Create field
drush field:create node.article.field_custom

# Delete field
drush field:delete node.article.field_custom

# List fields
drush field:list

Locale/Translation

# Import translations
drush locale:import de

# Update translations
drush locale:update

# Check translation status
drush locale:check

Queue Management

# Run specific queue
drush queue:run mymodule_queue

# List all queues
drush queue:list

# Delete items from queue
drush queue:delete mymodule_queue

Deployment Commands

# Standard deployment workflow
drush updatedb -y
drush config:import -y
drush cache:rebuild

# One-liner deployment
drush updb -y && drush cim -y && drush cr

Site Installation

# Install Drupal site
drush site:install standard --db-url=mysql://user:pass@localhost/dbname --account-name=admin --account-pass=admin

# Install with configuration
drush site:install --existing-config

Alias Usage

Drush aliases allow running commands on remote sites.

# Run command on remote site
drush @production cache:rebuild

# Sync database from remote to local
drush sql:sync @production @self

# Sync files from remote to local
drush rsync @production:%files @self:%files

Useful Flags

# -y: Assume "yes" to all prompts
drush en mymodule -y

# -v: Verbose output
drush cache:rebuild -v

# --uri: Specify site URI (multisite)
drush --uri=example.com cache:rebuild

# --root: Specify Drupal root directory
drush --root=/var/www/html cache:rebuild

# --debug: Show debug information
drush --debug cache:rebuild

Custom Drush Commands

Create custom Drush commands by implementing Drush command files in your module:

mymodule/src/Commands/MyModuleCommands.php

<?php

namespace Drupal\mymodule\Commands;

use Drush\Commands\DrushCommands;

/**
 * Custom Drush commands for mymodule.
 */
class MyModuleCommands extends DrushCommands {

  /**
   * Performs a custom operation.
   *
   * @command mymodule:custom-operation
   * @aliases mco
   * @usage mymodule:custom-operation
   *   Runs the custom operation.
   */
  public function customOperation() {
    $this->output()->writeln('Running custom operation...');
    // Your custom logic here
    $this->logger()->success('Operation completed successfully!');
  }

}

Best Practices

  1. Aliases: Create aliases in drush/sites/ for easier multi-site management
  2. Automation: Use Drush commands in deployment scripts
  3. Backup: Always backup before running destructive commands
  4. Testing: Test Drush scripts in development before production
  5. Version: Check Drush version compatibility with drush --version
  6. Performance: Use drush status to verify environment configuration