6

I'm wondering how to programmatically truncate a table in drupal8.

Like:

db_truncate('some_table');

Is this possible?

13

Sure, the function still exists: https://api.drupal.org/api/drupal/core%21includes%21database.inc/function/db_truncate/8.5.x.

It's deprecated, though, with the advice:

Instead, get a database connection injected into your service from the container and call truncate() on it. For example, $injected_database->truncate($table, $options);

If you're not in a context where injection is possible, get it from \Drupal:

\Drupal::database()->truncate('foo')->execute();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.