Chapter 59: Node.js MySQL Delete

DELETE operations in MySQL using Node.js.

I will explain it as if I am sitting next to you right now:

  • I open MySQL Workbench / terminal / DBeaver on one screen
  • I open VS Code on the other screen
  • I type every SQL line and every line of Node.js code live
  • I explain why we write it this way (security, performance, data integrity)
  • I show what 90% of beginners do wrong (and why it causes serious problems)
  • I show what intermediate developers often forget (and how it leads to bugs / data loss)
  • I show what real production code actually looks like in 2025–2026

Goal of this lesson

Learn how to write safe, clean, atomic, recoverable, logged DELETE operations from Node.js to MySQL.

We will cover:

  • Basic DELETE
  • DELETE with WHERE (single & multiple conditions)
  • DELETE with JOIN
  • DELETE with foreign key constraints (ON DELETE CASCADE vs RESTRICT)
  • Soft delete vs hard delete (real production choice)
  • Transaction usage (very important for safety)
  • Logging & auditing deleted records
  • Error handling & rollback
  • Performance & index considerations

Step 1 – Project setup (if you don’t have it yet)

Bash

tsconfig.json (strict & modern)

JSON

package.json scripts

JSON

src/config/database.ts (connection pool – must-have)

TypeScript

Step 2 – Create test tables (run this once in MySQL)

SQL

Important foreign key behavior

  • ON DELETE CASCADE → when user is deleted → all his tasks are automatically deleted
  • ON DELETE RESTRICT → prevents deleting user if he has tasks (safer in some cases)

Step 3 – Basic DELETE – delete one task

src/controllers/task.controller.ts

TypeScript

Route

TypeScript

Test it

Bash

Common beginner mistake

TypeScript

Correct → always use parameters

TypeScript

Step 4 – DELETE with ownership check (very important – security)

TypeScript

Why double-check ownership?

  • Prevents users from deleting other people’s tasks
  • Prevents IDOR (Insecure Direct Object Reference) attacks
  • Very common security bug in APIs

Step 5 – Soft delete instead of hard delete (production favorite)

Why soft delete?

  • You can recover deleted data
  • You can audit who deleted what and when
  • Easier compliance (GDPR, data retention policies)

Add deleted_at column

SQL

Soft delete controller

TypeScript

Select only active tasks

TypeScript

Why soft delete is preferred in production

  • You can restore data if user deletes by mistake
  • You can audit deletions (who, when)
  • Easier to comply with data retention laws
  • No need to worry about foreign key cascades when deleting users

Step 6 – Summary – DELETE / soft-delete best practices in Node.js + MySQL 2025–2026

Best Practice Why it matters Code pattern example
Use named placeholders :name Prevents SQL injection DELETE FROM tasks WHERE id = :id
Always check ownership Prevents IDOR / unauthorized deletion WHERE id = :id AND user_id = :userId
Prefer soft delete (deleted_at) Recoverable, auditable, GDPR-friendly UPDATE tasks SET deleted_at = NOW()
Use prepared statements Security – never concatenate values execute(sql, { id, userId })
Log deletions (audit trail) Compliance & debugging Insert into deleted_tasks table
Use transaction for multi-step delete Atomicity (all or nothing) pool.getConnection() + beginTransaction()
Use connection pool Performance + safety mysql2.createPool()
Add indexes on WHERE columns 10×–1000× faster DELETE / SELECT CREATE INDEX idx_user_id ON tasks(user_id)

Which part would you like to go much deeper into next?

  • Login + JWT authentication with MySQL
  • Full task CRUD (create/read/update/delete + ownership)
  • Add pagination + filtering + sorting + search
  • Replace raw SQL with Prisma (most popular choice today)
  • Replace raw SQL with Drizzle ORM (type-safe SQL)
  • Add Docker + docker-compose with real MySQL container
  • Soft-delete + restore + audit log complete system

Just tell me what you want to build or understand next — I’ll continue with complete, secure, production-ready code and explanations. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *