---
title: "An Easy Step-by-Step Guide to Writing Your Own Custom Drush 9 (and 10) Commands"
description: "Drush 9 (and 10) ships with a plethora of built-in commands to choose from. But you can come across situations when you need to create custom Drush 9 commands. Take a look at this helpful article to find an easy way to create your own custom Drush 9 commands."
url: https://www.specbee.com/blogs/writing-your-own-custom-drush-9-and-10-commands
type: Article
author: "Santhosh Kumar"
categories: "Code Corner"
published: 2021-09-16
updated: 2026-10-06
---

# An Easy Step-by-Step Guide to Writing Your Own Custom Drush 9 \(and 10\) Commands

If you’re a Drupal professional, Drush needs no introduction. But if you’re just starting out as a Drupal developer or keen on learning about Drupal, you should know that Drush is something you just CANNOT ignore. Short for “Drupal + Shell”, Drush is a Command Line Interface (CLI) tool made exclusively for Drupal. With Drush, you can set up new Drupal websites quickly and easily work with the Drupal installations. It helps you streamline your development and administrative tasks thus improving your productivity.

Drush 9 and Drush 10 core comes shipped with tons of helpful commands that helps you interact with themes, modules, profiles, etc. Some useful commands include – SQL commands, exporting or importing configurations, updates, migrations, cron or clear cache, and much more. It is also extremely extensible, in that you can create your own custom commands. In this article, you will find more information about how to create custom Drush 9 and 10 commands in an easy step-by-step process.

Note: Drupal 8.4 and higher support Drush 9, while Drupal 8.8 and above support Drush 10.

## Drush and Drupal Console

Although Drush and Drupal Console are both super useful CLI tools that make developers’ work easier, they are often used in different scenarios. With Drupal Console being the most recent addition to the Drupal world, it is a very useful tool for new developers to cope up with Drupal’s infamous learning curve. However, our take would be to use both Drush and Drupal Console as when put together they can do so much more in speeding up development and boosting productivity.

While Drupal Console lets you create custom modules, services, entities, boilerplate content, debugging and more, Drush lets you perform more basic but foundational tasks. These tasks include installing Drupal, interacting with the installation, exporting and importing [configurations](https://www.specbee.com/blogs/how-to-split-configurations-across-different-sites-in-drupal-10), download and update [contributed modules](https://www.specbee.com/blogs/top-drupal-9-modules-for-your-drupal-project), Caching, update the database, run cron jobs and much more.

## When would we need to Create Custom Drush commands?

Although there are many Drush 9 commands that are ready to use for various functionalities, there are always times when they’re not adequate. We create custom Drush commands for many database related processes like entity field value updates, DB updates, [importing or exporting data](https://www.specbee.com/blogs/export-import-menus-customblocks-taxonomies-drupal9-8-structure-sync-module) to and from Drupal, and other bulk processes. Also, when we have secure methods to be called, we can opt for Drush command implementation.

## How to Create a Custom Drush Command

The previous versions of Drush the implementation of Drush custom code followed different models. It was based on the **hook\_drush\_command()** and made use of .inc files.

In Drush 9, we will no longer use the .inc files or the **hook\_drush\_command()**. Drush commands will now be based on [Annotated command](https://github.com/consolidation/annotated-command) format. This changed the fundamental structure of custom Drush commands.

### Step 1: Create a module

Create a **.info.yml** file

 ![Custom-Drush.png ](https://www.specbee.com/sites/default/files/styles/large/public/migrated/Custom_Drush_0.png.avif?itok=LPJHR9ks)

 

 ### Step 2: Create a service using services.yml

Create a **.services.yml** file

 ![Services YML](https://www.specbee.com/sites/default/files/styles/large/public/migrated/Services_yml_0_0.png.avif?itok=hrmfycIv)

 

 ### Step 3: Create a Drush service class

Now let’s extend the DrushCommands base class.

 ![Use Drush](https://www.specbee.com/sites/default/files/styles/large/public/migrated/Use_Drush_0.png.avif?itok=IDIvtEbK)

 

 Path to the class file should be:

```plaintext
<module>/src/Commands/BatchCommands.php
```

 ![Batch Command](https://www.specbee.com/sites/default/files/styles/large/public/migrated/Batch_Command_0.png.avif?itok=a8-Wa_4A)

 

 Under this class, each method can be a command function if it is properly annotated.

[ ![Get UI UX services package now](https://www.specbee.com/sites/default/files/styles/large/public/migrated/Duck_20Duck_20go_20banner_57.jpg.avif?itok=tqSWATxH)

 

 ](https://www.specbee.com/services/drupal-seo)### Step 4: Create annotated methods

 ![Print Info](https://www.specbee.com/sites/default/files/styles/large/public/migrated/Print_info_0.png.avif?itok=3-g06-K1)

 

 Here are a few of the common annotated commands:

**@option** 
**@usage** 
**@command** 
**@param** 
**@process**
**@aliases** 
**@status**
**@extract**

### **Step 5: Clear cache**

Now let’s clear the cache to flush all caches with this command:

```plaintext
drush cr
```

Start using the new custom command you just created:

```plaintext
drush custom-message <message>
```

 ![Custom Message](https://www.specbee.com/sites/default/files/styles/large/public/migrated/Custom_Message_0.png.avif?itok=tIfRAkH9)

 

 Note: Multiple options added, as options is an array value.

The implementation of custom Drush commands will be useful in various scenarios like to run secure methods and bulk processes which exceed normal Drupal execution time and memory. For more complex and heavy processes, integrate [Drupal batch API](https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/9.0.x) in the Drush command and execute it. If you found this useful, consider [signing up to our newsletter](http://eepurl.com/gmWRBP) to have our regular articles get delivered straight to your inbox.

Looking for assistance in building compelling [Drupal websites](https://www.specbee.com/services/drupal-web-development) that can deliver successful results? [Get in touch](https://www.specbee.com/contact) with our Drupal experts today!