English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

NodeJS Basic Tutorial

NodeJS Express.js

NodeJS Buffer & URL;

NodeJS MySql

NodeJS MongoDB

NodeJS File (FS)

NodeJS Others

Node.js MySQL DELETE

Node.js MySQL delete records

The Node.js MySQL delete query can help you delete one or more records from the table based on filtering conditions.

  • MySQL delete some records based on filter

  • MySQL delete all records from table

Node.js MySQL – Delete records from table based on conditions

Execute a DELETE FROM query on the specified table when a filter is applied to one or more properties of the records in the table.

Content of the student table before deleting records

mysql> select * from students; 
+---------------+--------+-------+
| name | rollno | marks |
+---------------+--------+-------+
| John | 1 | 74 |
| Arjun | 2 | 74 |
| Prasanth | 3 | 77 |
| Adarsh | 4 | 78 |
| Raja | 5 | 94 |
| Sai | 6 | 84 |
| Ross | 7 | 54 |
| Monica Gellar | 8 | 86 |
| Lee | 9 | 98 |
| Bruce Wane | 10 | 92 |
| Sukumar | 11 | 99 |
| Anisha | 12 | 95 |
| Miley | 13 | 85 |
| Jobin | 14 | 87 |
| Jack | 16 | 82 |
| Priya | 17 | 88 |
+---------------+--------+-------+
16 rows in set (0.00 sec)
 // Import the mysql module 
 var mysql = require('mysql'); 
 
 // Create a connection variable with the required details 
 var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql 
  user: "arjun", // MySQL database username
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
 // Connect to the database. 
 con.connect(function (err) { 
  if (err) throw err; 
  // If the connection is successful 
  con.query("DELETE FROM students WHERE rollno>10" function (err, result, fields) { 
    // If any errors occur during the execution of the above query, throw an error 
    if (err) throw err; 
    // If there are no errors, you will get the result 
    console.log(result); 
  }); 
 });

Run deleteRecordsFiltered.js-Terminal Output

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node deleteRecordsFiltered.js 
OkPacket { 
  fieldCount: 0, 
  affectedRows: 6, 
  insertId: 0, 
  serverStatus: 34, 
  warningCount: 0, 
  message: '', 
  protocol41: true, 
  changedRows: 0 }

Observation results, affected behaviors6, which means that6records.

The following records remain after executing the MySQL DELETE FROM query on the student table.

Deleted student table content

mysql> select * from students; 
+---------------+--------+-------+
| name | rollno | marks |
+---------------+--------+-------+
| John | 1 | 74 |
| Arjun | 2 | 74 |
| Prasanth | 3 | 77 |
| Adarsh | 4 | 78 |
| Raja | 5 | 94 |
| Sai | 6 | 84 |
| Ross | 7 | 54 |
| Monica Gellar | 8 | 86 |
| Lee | 9 | 98 |
| Bruce Wane | 10 | 92 |
+---------------+--------+-------+
10 rows in set (0.00 sec)

Node.js MySQL – Delete All Records from Table

// Import the mysql module
var mysql = require('mysql'); 
 
// Create a connection variable with the required details
var con = mysql.createConnection({ 
  host: "localhost", // The IP address of the server running mysql
  user: "arjun", // MySQL database username
  password: "password", // The corresponding password
  database: "studentsDB" // Use the specified database
 }); 
 
// Connect to the database.
con.connect(function (err) { 
  if (err) throw err; 
  // If the connection is successful
  con.query("DELETE FROM students", function (err, result, fields) { 
    // If any errors occur during the execution of the above query, throw an error
    if (err) throw err; 
    // If there are no errors, you will get the result
    console.log(result); 
  }); 
 });

Run deleteRecordsAll.js-Terminal Output

arjun@arjun-VPCEH26EN:~/workspace/nodejs$ node deleteRecordsAll.js 
OkPacket { 
  fieldCount: 0, 
  affectedRows: 10, 
  insertId: 0, 
  serverStatus: 34, 
  warningCount: 0, 
  message: '', 
  protocol41: true, 
  changedRows: 0 }

The following is the content of the students table after execution

MySQL Table students Content

mysql> select * from students; 
Empty set (0.00 sec)

Conclusion:

In this Node.js tutorial – Node.js MySQL – DELETE FROM, we learned how to delete records based on conditions or delete all records from the table.