Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Drupal 8 || 9 : Truncate Watchdog table using Drush from a DDEV deploy.


When you're using DDEV, you can see the database from a PHPMyAdmin interface in: You can see all the tables there, included the log registers by default in Drupal, the so called "watchdog" table, with annotations about all the incidences in your Drupal installation. This table is managed by the Dblog module, present in Drupal core.

The watchdogtable contains columns as: uid (user triggering the event), the type of the log message, the severity of the event, a location url, a timestamp and many others.

Sometimes the watchdog table can grow out of control, so you can get a lot of errors from MySQL and/or slow down the performance of your Drupal installation. You must learn how to reduce the table, truncate it, delete certain records, etc. And perhaps you may have to turn off this module in Live / Production Environments.

Alternatives to the Dblog module in Drupal

Using SQL queries in Drupal

While the watchdog table is SQL-based, you can get or delete register using SQL queries as usual. Just like:

DELETE FROM table_name WHERE condition; 

In the Drupal context, this can be managed in two ways or styles: dynamic queries or static queries, for instance:

First, we gonna get a database connection object.

$connection = \Drupal::database();
// Or
$connection = \Drupal::service('database');

And then using a query builder object:

$num_deleted = $connection->delete('watchdog')
  ->condition('type', 'cron')

// Is just like: DELETE FROM {watchdog} WHERE type='cron';
// Dynamic queries in Drupal database API.

$database = \Drupal::database();
$query = $database->truncate('watchdog');

// Or

// Truncates the whole table.

Or by doing:

// Static queries in Drupal database API using db_query() function.  
$query = db_query("TRUNCATE TABLE watchdog");

// Or 
DELETE FROM {watchdog} WHERE type='cron
$query = db_query("DELETE FROM {watchdog} WHERE type = 'cron'");
$records = $query->fetchAll();

Read more about the Database API of Drupal:

Using Drupal services

You can delete registers in the watchdog table by using the database() service in Drupal:


Using Drush

When you're deploying environments using DDEV and wanna using drush:

$ ddev drush -y wd-del all

If you're working out of a DDEV deploy, then use only drush:

$ drush -y wd-del all

Delete messages in watchdog containing certain strings:

$ drush watchdog-delete "cron run succesful"

Delete all messages with severity of error:

$ drush watchdog-delete --severity=error

Delete all messages of type php:

$ drush watchdog-delete --type=php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment