Updated on June 15, 2022
You might think that using the drush command-line tool is only something hardcore developers do, but it turns out it's super-helpful for site builders and theme developers too! In my experience, using drush will speed up the usual Drupal Admin tasks 3 to 10 times, compared with visiting the Drupal admin pages in the browser.
In this article, I'll list the Drush commands I use every day in my Drupal theme development and site-building process. If you have more of them that you use regularly, please put them in the comments, as I would love to add them to the list.
As a requirement for executing all of the following commands, you must first ensure that you have Drush installed for your Drupal development environment. Fortunately, Acquia Dev Desktop, Pantheon.io, Drupal VM, and standard dev environments all come with drush pre-installed.
Without further ado, let's dive into my favourite commands, with both D7 and D8 versions where appropriate.
Clear the Caches
Whether you are loading new template files, adding new libraries or editing your main theme .info file's structure, you will always need a fast Cache clear.
Drupal 7
# run cache-clear on all caches $ drush cc all
Drupal 8
# run cache-rebuild $ drush cr
In my personal experience, you may need to clear Drupal Caches between 5 - 15 times a day.
Download and enable any contrib module
With more than 13,000+ Drupal 7 & 8 contrib modules at Drupal.org, the odds that you're not using any of them are pretty slim! Fortunately, drush makes it easy to install modules from Drupal.org using the following commands:
# Download a module $ drush dl pathauto # Enable the module $ drush en pathauto
Because smart themers are also lazy, they'll be happy to hear that you can do both the download and enabling in a single command:
$ drush en pathauto -y
Keep in mind, every drush command accepts a "-y" argument to skip all yes/no confirmations.
Import & Export Database Backups
The fastest way to backup your site data is doing a database dump. As a good practice, always clear the caches first, to keep the size of the database "dump file" to a minimum. Depending on the site structure and data, the difference can result in up to a 70% reduction on the SQL dump file size.
# Export the DB to a backup file $ drush cc $ drush sql-dump > dump.sql # Delete all data in your database (MAKE SURE YOU TRUST YOUR BACKUP!) $ drush sql-drop # Import the DB from a backup file $ drush sql-cli < dump.sql
Import & Export Site Configuration to Code
Until a site goes live, we do our best to develop without a database dump (Gasp!). When building a new dev or staging environment, we start from an empty database and migrate in all our sample content. Then we use either Features module (in Drupal 7) or Configuration Management (in Drupal 8) to import ALL site configuration (content types, fields, views, variables, and everything else) from the code. Here's how drush plays a role:
Using Features module
# Assuming you use Features site wide for Content Types, Contexts, Views... # Revert all enabled features by overriding current DB config with the features code $ drush features-revert-all # Update all feature modules code based on your current DB config $ drush features-update-all
Using Configuration Management
# Export the configuration to the "config/site" sample folder $ drush config-export --destination=config/site # Import the configuration from the "config/site" sample folder $ drush config-import --source=config/site
Downloading and Enabling the Drupal Theme
As with a module, you could manually visit Drupal.org, download the theme, extract it to the appropriate folder, then visit "Admin > Appearance" to set it. But after you see how easy it is with drush, you'll never do it any other way.
# Download a theme/module $ drush dl bootstrap # Enable the theme/module $ drush en bootstrap # List all Installed Themes $ drush pm-list --type=theme # Set the theme to be the "default one" or the "admin one" $ drush config-set system.theme default bootstrap $ drush config-set system.theme admin bootstrap
Update Drupal Core + Contrib Modules
Always keeping core and contrib modules up-to-date is the most tedious admin task I know. Turns out that drush has a single command that does everything for you! As a bonus, there's a second command that does a full site archive (code, database, and files) that you can restore from if anything goes wrong!
# Export the DB, code, and site into a timestamped and zipped tar archive $ drush archive-dump # Run the entire update process $ drush up
Generate Sample Content
As described by Blair Wadman in his blog post How to create dummy Drupal content with Drush and Devel, we can create a bunch of dummy nodes to test our Views, Display Modes and see the actual layout in action.
# Download and Enable Devel module $ drush en devel -y # Enable Devel Generate Sub-module $ drush en devel_generate -y # Generate 50 random nodes, with up to 10 comments each $ drush generate-content 50 10
Other Drush Capabilities...
As mentioned before, these are kind of the most popular Drush commands I use every day, but there's so much more to explore in the following resources:
Gratuitious terrible jokes
Finally, I learned that my colleague Alex Dergachev once did an intro Drush talk at a Drupalcamp Montreal 2010, where he organized his slides around sinister-sounding "Soviet drussia" jokes. We're including these here for posterity.
In Soviet Drussia...
- Dog watches you
- Cron executes you
- Variable gets you
- PHP evaluates you
- Core hacks you
- Module uninstalls you
- Caches clear you
- Content manages you
- Terrible joke ruins you
Let us know if he missed any stinkers.