Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Drush Commands Tutorial

Introduction to Drush

Drush is a command-line shell and scripting interface for Drupal. It allows developers and site administrators to perform various tasks quickly and efficiently without having to navigate through the Drupal administrative interface. With Drush, you can manage your Drupal site from the command line, making it an essential tool for anyone working with Drupal.

Installing Drush

Before using Drush, you need to install it. The recommended way to install Drush is through Composer. Here’s how to do it:

composer global require drush/drush

After installation, ensure that your Composer's global bin directory is in your system's PATH. You can check if Drush is installed by running:

drush --version

Basic Drush Commands

Drush provides a variety of commands to manage your Drupal site. Here are some of the most commonly used commands:

1. Clearing Cache

Clearing the cache is a common task during development. You can do this with the following command:

drush cache-rebuild

This command rebuilds all caches, useful after making changes to your code or configuration.

2. Running Database Updates

To apply pending database updates after module installations or updates, use:

drush updatedb

3. Enabling and Disabling Modules

You can enable a module using:

drush en module_name

And disable it using:

drush pm-uninstall module_name

Advanced Drush Commands

For more advanced tasks, Drush offers a number of powerful commands:

1. Running Cron

To run cron tasks, which are scheduled tasks that Drupal performs, use:

drush cron

2. Exporting and Importing Configuration

To export your site's configuration, use:

drush config-export

And to import configuration:

drush config-import

Using Drush Aliases

Drush allows you to create aliases for your Drupal sites, which can be especially useful for managing multiple environments (e.g., local, staging, production). To create an alias, edit your Drush aliases file, usually located at:

~/.drush/sites/aliases.drushrc.php

Here’s an example of an alias:

$aliases['prod'] = array(
    'uri' => 'example.com',
    'root' => '/path/to/drupal',
    'db-url' => 'mysql://user:pass@localhost/dbname',
);
                

After setting up your alias, you can run commands against the alias like this:

drush @prod status

Conclusion

Drush is an invaluable tool for Drupal developers and site administrators. From basic tasks like clearing caches to advanced operations with configuration management, it enhances efficiency and productivity. By mastering Drush commands, you can significantly streamline your Drupal workflows.