Node js MySQL Delete Query Example

In this post, we will see how to delete a record from the MySQL database using Node js.

Setup MySQL database

Make sure that you have installed the MySQL database in your machine.
Use the following command to create a database:
create database demo;
Use below SQL script to create todos table in 'demo' database:
CREATE TABLE `todos` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)

Install MySQL Driver

To download and install the "mysql" module, open the Command Terminal and execute the following:
$npm install mysql --save
Now you have downloaded and installed a MySQL database driver.
Node.js can use this module to manipulate the MySQL database:
var mysql = require('mysql');

Node js MySQL Delete Query Example

Use the below examples to insert and select records with the database.

Deleting Data

Let's create a file named 'delete-record.js' and add the following code to it:
const mysql = require('mysql');
// connection configurations
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'demo'

// connect to database
connection.connect(function (err) {
    if (err) throw err
    console.log('You are now connected with mysql database...')

let params = {
    name: "Todo 1",
    description: "Todo 1 description"

    'DELETE FROM todos where id = ?', [3], (err, result) => {
        if (err) throw err;
Run above code with the following command:
$ node .\delete-record.js
You are now connected with mysql database...
OkPacket {
  fieldCount: 0,
  affectedRows: 0,
  insertId: 0,
  serverStatus: 2,
  warningCount: 0,
  message: '',
  protocol41: true,
  changedRows: 0 }


Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course