Backend and Infrastructure

Overview: Creating Your Own Custom Drush Commands for Drupal 8, 9, and 10

Developers can implement the Drush command API to write their own custom Drush commands. This allows you to include Drush commands with your modules to allow the module's features to be used via the CLI. You can also create project-specific Drush commands that help with the development, deployment, and maintenance of your particular application.

We've written Drush commands to help generate reports, make it easier for new team members to get up and running, compile custom theme assets, and more. Any time we need to write PHP code that interacts with our Drupal site where we're worried the code might time out because it takes too long to execute we'll reach for Drush. Custom Drush commands are also useful to combine background processes that can be executed on cron, such as nightly imports, data synchronization, bulk database manipulation, custom queues processing, and so much more.

In this tutorial we'll:

  • Learn about different types of custom Drush commands
  • Review the anatomy of a Drush command
  • See how the Drush bootstrap process relates to commands

By the end of this tutorial, you'll be able to identify the parts of a custom Drush command and start writing your own.