Node.js MySQL Delete

🚀 Node.js MySQL – DELETE Records

The DELETE statement is used to remove records from a MySQL table.

We’ll use mysql2 (recommended) for all examples.


1. Install mysql2

npm install mysql2

2. Connect to Database


 


3. Delete a Single Record by ID


 

Output Example:

Deleted Rows: 1

4. Delete Multiple Records Using Conditions


 


5. Delete All Records

⚠️ Be careful! This removes all rows from the table.



 


6. Using Async/Await (Recommended)


 


7. Tips & Best Practices

  • Always use WHERE unless you intend to delete all rows.

  • Use prepared statements (?) to prevent SQL injection.

  • Wrap in try/catch when using async/await to catch errors.

  • Consider soft delete (mark as deleted) for critical data instead of hard delete.

You may also like...