Truncate all cache tables with drush or code

Drush

echo "SHOW TABLES LIKE 'cache%'" | $(drush sql-connect) | tail -n +2 | xargs -L1 -I% echo "TRUNCATE TABLE %;" | $(drush sql-connect) -v

Here's how to do this from the command line...

mysql -uUSER -pPASS --execute="SELECT concat('TRUNCATE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'DB_NAME' AND TABLE_NAME LIKE 'cache%'" | sed 1d | mysql -uUSER -pPASS DB_NAME

Modify this code to make it with code

global $conf;
$result = db_query("SHOW TABLES LIKE 'cache%'");
while ($table_name = db_result($result)) {
  if ($table_name == 'cache_form' || (!empty($conf['memcache_bins'][$table_name]) && $conf['memcache_bins'][$table_name] == 'database')) {
    echo t('%name NOT truncated.', array('%name' => $table_name)) . "\n";
    continue;
  }
  db_query("TRUNCATE TABLE %s", $table_name);
  echo t('%name truncated.', array('%name' => $table_name)) . "\n";
}