Node.js MySQL Delete – How to delete data Using Node.js and MySQL

Node.js MySQL Delete: This tutorial will help you to delete the data of the MYSQL table row. it will be very useful for you. Even you will get the best & easy Node.js MySQL Delete Query with a Simple example. So, Read all the next steps and use the given concept in your project.

node.js mysql delete data

Node.js MySQL Delete Query with Example

Before start coding to delete data, you must insert data into MYSQL Database and display data from the MySQL database. Otherwise, you may not understand this tutorial properly.

1. Install Express Application

You have to Install Basic Express Application and create the following folders and files

myapp/
  |__routes/
  |    |__users.js
  |__views/
  |    |__user-list.ejs
  |__app.js
  |__database.js
  |

Note – If you have already installed the express application then you need not do it again

2. Connect Node.js App to MySQL

You must make sure the MySQL module is installed and included in the database connection file. After that, write the connection script in the database.js

File Name – database.js

var mysql = require('mysql');
var conn = mysql.createConnection({
  host: 'localhost', // Replace with your host name
  user: 'root',      // Replace with your database username
  password: '',      // Replace with your database password
  database: 'nodeapp' // // Replace with your database Name
}); 
conn.connect(function(err) {
  if (err) throw err;
  console.log('Database is connected successfully !');
});
module.exports = conn;

3. Create Delete Link Button

You have to create a delete button link for each record in the HTML table. So, to do it, you have to display all the records in an HTML table and just add the following line to delete data in user-list.ejs  –

<td><a href="/users/delete/<%=data.id%>">Delete</a></td>

4. Create Routes to Delete Data

Configure the following points to create routes for deleting data –

  • Include database connection file database.js
  • Create a route /delete/:id with the GET method to display data in the HTML form.
  • Write SQL query to delete data.
  • If the query returns true then redirect to /users/user-list.

File Name – users.js

router.get('/delete/:id', function(req, res, next) {
  var id= req.params.id;
    var sql = 'DELETE FROM users WHERE id = ?';
    db.query(sql, [id], function (err, data) {
    if (err) throw err;
    console.log(data.affectedRows + " record(s) updated");
  });
  res.redirect('/users/user-list');
  
});

Also, make sure that Database Name is nodeapp and Table Name is users.  If you want to implement this script to your own database & table name of your project then you can do it. But I suggest you should execute it as it is given if you are a learner. after that, you can use it in your project.

5. Load Route File into Root File

You must load the route file users.js into the root file app.js. If users.js already existed then you need not create it again.

File Name – app.js

var usersRouter = require('./routes/users');
;
app.use('/users', usersRouter);

Note – If the above code already exists in the app.js  then you can skip this step.

6. Run Node.js Code to update data 

To Run Node.js code for deleting data, you will have to do the following things –

  • First of all, Start your Node.js server using npm start command
  • Enter the following URL into your web browser to display records in an HTML table
http://localhost:3000/users/user-list

When you click the delete link button, data will be deleted successfully based on the passing id from the delete link button.

You have learned to delete simple data from the database. Now you can implement the concept of this tutorial to any kind of table data.

My Suggestion

Dear developers, I hope you have understood the above script, Now you are able to delete data using Node.js & MySQL.Even you can delete another record by using the above steps.

If you have any questions or suggestions regarding Node.js. You can directly ask me through the below comment box.

2 thoughts on “Node.js MySQL Delete – How to delete data Using Node.js and MySQL”

    • Click here to get code arrangement in EJS for a delete query. First of all, you should insert & fetch data by reading from my tutorials then you will completely understand the code arrangement.

Comments are closed.