Drupal 7: Basic Drush Commands

31 Kudos

If you have SSH (command line access) to your Drupal website, then drush is a must.  Here are a few of my favourite and most useful Drush commands.

1. Clearing the cache with drush

When developing clearing the cache can save a lot of headaches, however doing it through the user interface takes it’s time and using drush helps to speed up this process.

drush cc all

It is also possible to pick the specific cache you wish to clear but most of the time I am using this command in development and clearing all the caches works perfectly.

2. Downloading and enabling modules with drush

This is one of the best features of drush and probably the most used

drush dl <project_name> -y

drush en <project_name> -y

It is also possible to specify the version you wish to download, for example if you are looking for the development version

drush dl <project_name>-7.x-1.x-dev -y

3. Disabling and uninstalling modules with drush

Another feature which allows quick disabling of modules including their dependencies all in one line

drush dis <project_name> -y

drush pm-uninstall <project_name> -y

Don’t forget you can chain commands together in the normal bash way

drush dis <project_name> && drush pm-uninstall <project_name> && drush updb && drush cc all && rm -rf path/to/project/folder

4. Resetting user passwords with drush

Sometimes you need to reset passwords for specific users or even the admin password, in Drupal 7, passwords are salted and must be created with the password script, where in Drupal 6, the passwords were just a MD5 hash. Thankfully drush has a command to automate the whole process

drush upwd <username> --password

Another useful feature is generating a password reset link

drush uli <username>

5. Setting maintenance mode with drush

The vset command allows you to modify variables in the variable table from the command line, it follows this general rule

drush vset <variable_name> <value>

Which means to change the maintenance_mode variable on, we use this command

drush vset maintenance_mode 1

To change the maintenance_mode variable off, we use this command

drush vset maintenance_mode 0

6. Changing the default theme with drush

Have you ever blown up your site with a bad change but need to get into the interface so you can change something back? Well whatever reason you have where you need to change the default theme, this command can save lives.

drush vset theme_default <theme_name>

The theme must be enabled, but you can do with with the drush en command listed above.

7. List all enabled modules, exclude core with drush

drush pml --status=enabled --type=module
drush pml --status=enabled --type=module --no-core

8. MySQL dump with drush

Perform a mysqldump (or equivalent) of the connected drupal database into a .sql file

drush sql-dump --result-file=/home/user/dump.sql

9. Backup and restore sites with drush

You backup your site into a single archive file, and then restore it with these simple commands

drush archive-dump --destination=/home/user/backup.tar.gz

drush archive-restore /home/user/backup.tar.gz

10. Update drush with drush

A very simple command to get the latest version of drush

drush self-update

For a full list of commands, check out http://drush.ws/

Also read...