Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • πŸ“ Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • πŸ“ Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • πŸ“ Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • πŸ“ Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • πŸ“ While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • πŸ“ For Each Assignments
    • πŸ“ All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • πŸ“ Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • πŸ“ Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • πŸ“ Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • πŸ“ Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • πŸ“ Assignments
  • Exception
    • Error vs Exception
    • Types of Exception
    • Exception Handling
    • User Defined Exception
    • Logging Exception
    • πŸ“ Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • πŸ“ Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • πŸ“ Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • πŸ“ Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • πŸ“ Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • πŸ“ Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • πŸ“ Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • πŸ“ Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • πŸ“ Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • πŸ“ Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • πŸ“ Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • πŸ“ Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • πŸ“ Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • πŸ“ Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

Python: Delete Data in MySQL

Python MySQL Delete Data - Complete Guide

Learn how to safely delete data from MySQL tables using Python.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers, data engineers, and data scientists to make programming easy through practical examples, real-world experience, and clear explanations.
On this page:
  • What is DELETE?
  • DELETE Syntax
  • Deleting Single Rows
  • Deleting Multiple Rows
  • Deleting All Rows
  • Safety Measures
  • Real-World Example
  • Best Practices
  • Try It Yourself
  • Quiz
  • FAQ
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
What You'll Learn Here
  • What is DELETE β€” removing data from MySQL tables
  • DELETE syntax β€” how to write DELETE statements
  • Single row deletion β€” removing specific records
  • Multiple row deletion β€” removing many records at once
  • Safety measures β€” preventing accidental data loss
  • Best practices β€” safe deletion guidelines

What is DELETE?

The DELETE statement is used to remove existing data from a MySQL table. It's the most dangerous operation because once data is deleted, it's usually gone forever.

⚠️ WARNING: DELETE is permanent! Unlike INSERT or UPDATE, there's no easy way to undo a DELETE. Always be careful when deleting data.

Think of DELETE like throwing away a file from your computer. Once you empty the recycle bin, it's gone. DELETE does the same thing with your database records.

1

When to Use DELETE

# ============================================================
# REAL-WORLD SCENARIOS FOR DELETE
# ============================================================

# 1. Remove a user who closed their account
DELETE FROM students WHERE student_id = 5;

# 2. Delete old or archived records
DELETE FROM orders WHERE order_date < '2023-01-01';

# 3. Remove duplicate records
DELETE FROM students WHERE student_id IN (...);

# 4. Clean up test data
DELETE FROM students WHERE is_test = 1;

# 5. Remove inactive users
DELETE FROM students WHERE last_login < '2022-01-01';

# ============================================================
# IMPORTANT: DELETE vs TRUNCATE
# ============================================================

# DELETE - removes rows one by one (can use WHERE)
DELETE FROM students WHERE age > 30;

# TRUNCATE - removes all rows at once (faster, no WHERE)
TRUNCATE TABLE students;

# Use TRUNCATE when you want to delete ALL rows
# Use DELETE when you want to delete SPECIFIC rows

Key point: DELETE removes data. TRUNCATE removes all data. Both are permanent operations.

Quick Check: What is the difference between DELETE and TRUNCATE? (Answer: DELETE can use WHERE to delete specific rows; TRUNCATE deletes ALL rows)

DELETE Syntax

2

Basic DELETE Syntax

# ============================================================
# BASIC DELETE SYNTAX
# ============================================================

DELETE FROM table_name WHERE condition;

# ============================================================
# REAL EXAMPLES
# ============================================================

# 1. Delete a specific student
DELETE FROM students WHERE student_id = 5;

# 2. Delete students younger than 18
DELETE FROM students WHERE age < 18;

# 3. Delete students with a specific name
DELETE FROM students WHERE first_name = 'Test';

# 4. Delete students who haven't logged in recently
DELETE FROM students WHERE last_login < '2023-01-01';

# ============================================================
# IMPORTANT: ALWAYS USE WHERE!
# ============================================================

# ❌ DANGEROUS - Deletes ALL rows!
DELETE FROM students;

# βœ… SAFE - Deletes only matching rows
DELETE FROM students WHERE student_id = 5;

# ============================================================
# IN PYTHON
# ============================================================

import mysql.connector

connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="secret",
    database="myapp_db"
)

cursor = connection.cursor()

# Delete a specific student
query = "DELETE FROM students WHERE student_id = %s"
cursor.execute(query, (5,))
connection.commit()

print(f"Deleted {cursor.rowcount} row(s)")

cursor.close()
connection.close()

DELETE structure:

  • DELETE FROM β€” specifies the table
  • WHERE β€” specifies which rows to delete
  • Important! Without WHERE, ALL rows are deleted
  • Always test with SELECT first

Quick Check: What happens if you forget the WHERE clause in DELETE? (Answer: ALL rows in the table will be deleted)

Deleting Single Rows

3

Removing Specific Records

# ============================================================
# DELETING A SINGLE ROW
# ============================================================

import mysql.connector

connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="secret",
    database="myapp_db"
)

cursor = connection.cursor()

# ============================================================
# METHOD 1: Delete by ID (Safest)
# ============================================================

def delete_student(student_id):
    """Delete a student by their ID"""
    query = "DELETE FROM students WHERE student_id = %s"
    cursor.execute(query, (student_id,))
    connection.commit()
    return cursor.rowcount

# Delete student with ID 5
rows_deleted = delete_student(5)
if rows_deleted > 0:
    print(f"βœ… Deleted student ID {5}")
else:
    print(f"⚠️ No student found with ID {5}")

# ============================================================
# METHOD 2: Delete by unique email
# ============================================================

def delete_student_by_email(email):
    """Delete a student by their email"""
    query = "DELETE FROM students WHERE email = %s"
    cursor.execute(query, (email,))
    connection.commit()
    return cursor.rowcount

# Delete student with specific email
rows_deleted = delete_student_by_email("test@email.com")
if rows_deleted > 0:
    print(f"βœ… Deleted student with email test@email.com")
else:
    print(f"⚠️ No student found with that email")

# ============================================================
# METHOD 3: Delete with confirmation
# ============================================================

def safe_delete_student(student_id):
    """Delete a student with confirmation"""
    # First, check if student exists
    cursor.execute("SELECT * FROM students WHERE student_id = %s", (student_id,))
    student = cursor.fetchone()
    
    if not student:
        print(f"⚠️ Student ID {student_id} not found")
        return 0
    
    # Show what will be deleted
    print(f"About to delete: {student}")
    
    # Ask for confirmation
    confirm = input(f"Are you sure you want to delete student ID {student_id}? (y/n): ")
    if confirm.lower() == 'y':
        cursor.execute("DELETE FROM students WHERE student_id = %s", (student_id,))
        connection.commit()
        print(f"βœ… Deleted student ID {student_id}")
        return cursor.rowcount
    else:
        print(f"❌ Deletion cancelled")
        return 0

# Use the safe delete
safe_delete_student(10)

cursor.close()
connection.close()

Single row deletion tips:

  • Use a unique identifier (like student_id) in WHERE
  • Always commit after DELETE
  • Check cursor.rowcount to see if anything was deleted
  • Consider confirming before deleting

Quick Check: What should you use in the WHERE clause to delete a single row? (Answer: A unique identifier like student_id or primary key)

Deleting Multiple Rows

4

Removing Many Records at Once

# ============================================================
# DELETING MULTIPLE ROWS
# ============================================================

import mysql.connector

connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="secret",
    database="myapp_db"
)

cursor = connection.cursor()

# ============================================================
# EXAMPLE 1: Delete by age range
# ============================================================

def delete_by_age_range(min_age, max_age):
    """Delete students in an age range"""
    query = "DELETE FROM students WHERE age BETWEEN %s AND %s"
    cursor.execute(query, (min_age, max_age))
    connection.commit()
    return cursor.rowcount

# Delete students aged 18-20
rows_deleted = delete_by_age_range(18, 20)
print(f"βœ… Deleted {rows_deleted} students aged 18-20")

# ============================================================
# EXAMPLE 2: Delete by condition
# ============================================================

def delete_inactive_students():
    """Delete students who haven't logged in recently"""
    query = "DELETE FROM students WHERE last_login < '2023-01-01'"
    cursor.execute(query)
    connection.commit()
    return cursor.rowcount

# Delete inactive students
rows_deleted = delete_inactive_students()
print(f"βœ… Deleted {rows_deleted} inactive students")

# ============================================================
# EXAMPLE 3: Delete using IN operator
# ============================================================

def delete_by_ids(student_ids):
    """Delete students with specific IDs"""
    placeholders = ', '.join(['%s'] * len(student_ids))
    query = f"DELETE FROM students WHERE student_id IN ({placeholders})"
    cursor.execute(query, student_ids)
    connection.commit()
    return cursor.rowcount

# Delete students with IDs 10, 12, 15
rows_deleted = delete_by_ids([10, 12, 15])
print(f"βœ… Deleted {rows_deleted} students")

# ============================================================
# SAFETY: Check before deleting
# ============================================================

def safe_delete_with_check(condition_query, delete_query, params):
    """Check what will be deleted before actually deleting"""
    # First, see what will be deleted
    cursor.execute(condition_query, params)
    rows = cursor.fetchall()
    
    if not rows:
        print("⚠️ No rows found to delete")
        return 0
    
    print(f"⚠️ About to delete {len(rows)} rows:")
    for row in rows:
        print(f"   {row}")
    
    confirm = input("Are you sure you want to delete these rows? (y/n): ")
    if confirm.lower() == 'y':
        cursor.execute(delete_query, params)
        connection.commit()
        print(f"βœ… Deleted {cursor.rowcount} rows")
        return cursor.rowcount
    else:
        print("❌ Deletion cancelled")
        return 0

# Example usage
safe_delete_with_check(
    "SELECT * FROM students WHERE age > 30",
    "DELETE FROM students WHERE age > 30",
    ()
)

cursor.close()
connection.close()

Multiple row deletion tips:

  • Be extra careful with multiple row deletions
  • Always test with SELECT first
  • Use transactions for safety
  • Consider backup before major deletions

Quick Check: How can you test which rows will be deleted before running DELETE? (Answer: Use a SELECT statement with the same WHERE clause)

Deleting All Rows

5

Clearing an Entire Table

⚠️ WARNING: This section shows how to delete ALL rows. This is DANGEROUS and should only be done with extreme caution!

# ============================================================
# DELETING ALL ROWS
# ============================================================

import mysql.connector

connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="secret",
    database="myapp_db"
)

cursor = connection.cursor()

# ============================================================
# METHOD 1: DELETE ALL (SLOW but can use WHERE)
# ============================================================

def delete_all_students():
    """Delete all students - BAD IDEA unless you're sure!"""
    # Show warning
    print("⚠️⚠️⚠️ WARNING: This will delete ALL students! ⚠️⚠️⚠️")
    
    # Show count
    cursor.execute("SELECT COUNT(*) FROM students")
    count = cursor.fetchone()[0]
    print(f"πŸ“Š There are {count} students in the table")
    
    # Ask for confirmation
    confirm = input("Are you REALLY sure? Type 'YES' to continue: ")
    if confirm == 'YES':
        cursor.execute("DELETE FROM students")
        connection.commit()
        print(f"βœ… Deleted ALL {cursor.rowcount} students")
        return cursor.rowcount
    else:
        print("❌ Deletion cancelled")
        return 0

# ============================================================
# METHOD 2: TRUNCATE (FASTER but can't use WHERE)
# ============================================================

def truncate_students():
    """Truncate the students table - even faster!"""
    print("⚠️⚠️⚠️ WARNING: This will TRUNCATE the entire table! ⚠️⚠️⚠️")
    
    confirm = input("Are you REALLY sure? Type 'YES' to continue: ")
    if confirm == 'YES':
        cursor.execute("TRUNCATE TABLE students")
        connection.commit()
        print("βœ… Table truncated (all rows removed)")
        return True
    else:
        print("❌ Truncation cancelled")
        return False

# ============================================================
# COMPARISON: DELETE vs TRUNCATE
# ============================================================

print("\nπŸ“Š DELETE vs TRUNCATE:")
print("β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”")
print("β”‚ DELETE           β”‚ TRUNCATE        β”‚ DIFFERENCE      β”‚")
print("β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€")
print("β”‚ Can use WHERE   β”‚ No WHERE        β”‚ DELETE is safer β”‚")
print("β”‚ Slower          β”‚ Faster          β”‚ TRUNCATE is faster")
print("β”‚ Can rollback    β”‚ Cannot rollback β”‚ DELETE is safer β”‚")
print("β”‚ Returns rowcountβ”‚ No rowcount     β”‚ DELETE gives info")
print("β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜")

# ============================================================
# SAFER APPROACH: Delete in batches
# ============================================================

def delete_in_batches(batch_size=1000):
    """Delete all rows in batches (safer for large tables)"""
    total_deleted = 0
    
    while True:
        cursor.execute(f"DELETE FROM students LIMIT {batch_size}")
        connection.commit()
        rows_deleted = cursor.rowcount
        
        if rows_deleted == 0:
            break
        
        total_deleted += rows_deleted
        print(f"Deleted {rows_deleted} rows (Total: {total_deleted})")
    
    print(f"βœ… Finished. Deleted {total_deleted} rows total")
    return total_deleted

cursor.close()
connection.close()

Deleting all rows - key points:

  • DELETE FROM table β€” deletes all rows (slow, can be rolled back)
  • TRUNCATE TABLE β€” deletes all rows (fast, cannot be rolled back)
  • Always confirm before deleting all rows
  • Consider batching for large tables

Quick Check: What is the difference between DELETE FROM table and TRUNCATE TABLE? (Answer: DELETE can be rolled back and is slower; TRUNCATE is faster but cannot be rolled back)

Safety Measures

6

Protecting Your Data

# ============================================================
# SAFETY MEASURES FOR DELETE
# ============================================================

print("=" * 60)
print("SAFETY MEASURES FOR DELETE OPERATIONS")
print("=" * 60)

# ============================================================
# 1. ALWAYS TEST WITH SELECT FIRST
# ============================================================

print("\n1. TEST WITH SELECT FIRST")
print("   # See what will be deleted")
print("   SELECT * FROM students WHERE age > 30")
print("   # Then delete")
print("   DELETE FROM students WHERE age > 30")

# ============================================================
# 2. USE TRANSACTIONS
# ============================================================

print("\n2. USE TRANSACTIONS")
print("   # Start transaction")
print("   connection.start_transaction()")
print("   # Delete")
print("   cursor.execute('DELETE FROM students WHERE student_id = 5')")
print("   # Verify")
print("   if okay: connection.commit()")
print("   else: connection.rollback()")

# ============================================================
# 3. BACKUP BEFORE DELETING
# ============================================================

print("\n3. BACKUP BEFORE DELETING")
print("   # Backup the table")
print("   CREATE TABLE students_backup AS SELECT * FROM students")
print("   # Or backup specific data")
print("   CREATE TABLE students_to_delete AS SELECT * FROM students WHERE age > 30")

# ============================================================
# 4. USE SOFT DELETE
# ============================================================

print("\n4. USE SOFT DELETE")
print("   # Instead of deleting, mark as deleted")
print("   ALTER TABLE students ADD COLUMN is_deleted BOOLEAN DEFAULT 0")
print("   # 'Delete' by updating")
print("   UPDATE students SET is_deleted = 1 WHERE student_id = 5")
print("   # Filter out deleted records")
print("   SELECT * FROM students WHERE is_deleted = 0")

# ============================================================
# 5. DOUBLE CHECK
# ============================================================

print("\n5. DOUBLE CHECK YOUR WHERE CLAUSE")
print("   # Always double check your WHERE clause")
print("   # Make sure you're deleting the right rows")

# ============================================================
# 6. LIMIT DELETIONS
# ============================================================

print("\n6. LIMIT DELETIONS FOR SAFETY")
print("   # Delete in small batches")
print("   DELETE FROM students WHERE age > 30 LIMIT 100")

# ============================================================
# IMPLEMENTATION: SafeDelete Class
# ============================================================

class SafeDelete:
    """Helper class for safe deletion operations"""
    
    def __init__(self, cursor, connection):
        self.cursor = cursor
        self.connection = connection
    
    def preview(self, query, params=None):
        """Preview what will be deleted"""
        select_query = query.replace("DELETE", "SELECT *")
        self.cursor.execute(select_query, params)
        return self.cursor.fetchall()
    
    def delete_with_confirmation(self, delete_query, params=None, message="Are you sure?"):
        """Delete with user confirmation"""
        # Preview
        rows = self.preview(delete_query, params)
        
        if not rows:
            print("No rows found to delete")
            return 0
        
        print(f"About to delete {len(rows)} rows")
        for row in rows[:5]:  # Show first 5
            print(f"  {row}")
        if len(rows) > 5:
            print(f"  ... and {len(rows) - 5} more")
        
        confirm = input(f"{message} (y/n): ")
        if confirm.lower() == 'y':
            self.cursor.execute(delete_query, params)
            self.connection.commit()
            return self.cursor.rowcount
        else:
            print("Deletion cancelled")
            return 0

Safety measures summary:

  • Test with SELECT β€” verify what will be deleted
  • Use transactions β€” commit or rollback
  • Backup β€” save data before deleting
  • Soft delete β€” mark instead of actually deleting
  • Double check β€” verify your WHERE clause
  • Limit deletions β€” delete in batches

Quick Check: What is a soft delete? (Answer: Marking records as deleted instead of actually removing them)

Real-World Example: User Management

7

Building a User Management System with Safe Deletion

# ============================================================
# USER MANAGEMENT WITH SAFE DELETION
# ============================================================

import mysql.connector

class UserManager:
    """Manage users with safe deletion features"""
    
    def __init__(self, db_config):
        self.db_config = db_config
        self.connection = None
        self.cursor = None
    
    def connect(self):
        try:
            self.connection = mysql.connector.connect(**self.db_config)
            self.cursor = self.connection.cursor()
            return True
        except mysql.connector.Error as e:
            print(f"Connection failed: {e}")
            return False
    
    def get_user(self, user_id):
        """Get a user by ID"""
        query = "SELECT * FROM students WHERE student_id = %s"
        self.cursor.execute(query, (user_id,))
        return self.cursor.fetchone()
    
    def delete_user(self, user_id, confirm=False):
        """Delete a user (with confirmation)"""
        # Check if user exists
        user = self.get_user(user_id)
        if not user:
            print(f"User ID {user_id} not found")
            return 0
        
        print(f"About to delete user: {user}")
        
        if confirm:
            query = "DELETE FROM students WHERE student_id = %s"
            self.cursor.execute(query, (user_id,))
            self.connection.commit()
            print(f"βœ… Deleted user ID {user_id}")
            return self.cursor.rowcount
        else:
            print("❌ Deletion requires confirmation")
            return 0
    
    def delete_inactive_users(self, days, confirm=False):
        """Delete users inactive for more than X days"""
        query = "DELETE FROM students WHERE last_login < DATE_SUB(CURDATE(), INTERVAL %s DAY)"
        
        if confirm:
            self.cursor.execute(query, (days,))
            self.connection.commit()
            print(f"βœ… Deleted {self.cursor.rowcount} inactive users")
            return self.cursor.rowcount
        else:
            print("❌ Deletion requires confirmation")
            return 0
    
    def soft_delete_user(self, user_id):
        """Soft delete a user (mark as deleted)"""
        query = "UPDATE students SET is_deleted = 1 WHERE student_id = %s"
        self.cursor.execute(query, (user_id,))
        self.connection.commit()
        print(f"βœ… Soft deleted user ID {user_id}")
        return self.cursor.rowcount
    
    def restore_user(self, user_id):
        """Restore a soft-deleted user"""
        query = "UPDATE students SET is_deleted = 0 WHERE student_id = %s"
        self.cursor.execute(query, (user_id,))
        self.connection.commit()
        print(f"βœ… Restored user ID {user_id}")
        return self.cursor.rowcount
    
    def bulk_delete(self, user_ids, confirm=False):
        """Delete multiple users at once"""
        if not user_ids:
            return 0
        
        placeholders = ', '.join(['%s'] * len(user_ids))
        query = f"DELETE FROM students WHERE student_id IN ({placeholders})"
        
        if confirm:
            self.cursor.execute(query, user_ids)
            self.connection.commit()
            print(f"βœ… Deleted {self.cursor.rowcount} users")
            return self.cursor.rowcount
        else:
            print("❌ Deletion requires confirmation")
            return 0
    
    def close(self):
        if self.cursor:
            self.cursor.close()
        if self.connection:
            self.connection.close()

# ============================================================
# DEMONSTRATION
# ============================================================

db_config = {
    "host": "localhost",
    "user": "root",
    "password": "secret",
    "database": "myapp_db"
}

manager = UserManager(db_config)
if manager.connect():
    
    # 1. Get user before deletion
    user = manager.get_user(5)
    if user:
        print(f"User before: {user}")
    
    # 2. Delete with confirmation
    # This would only work with confirm=True
    # rows = manager.delete_user(5, confirm=True)
    
    # 3. Soft delete
    rows = manager.soft_delete_user(5)
    print(f"Soft deleted {rows} user(s)")
    
    # 4. Restore
    rows = manager.restore_user(5)
    print(f"Restored {rows} user(s)")
    
    # 5. Bulk delete
    # rows = manager.bulk_delete([10, 12, 15], confirm=True)
    
    manager.close()

This example shows:

  • User lookup before deletion
  • Confirmation-based deletion
  • Soft delete (marking instead of removing)
  • Restoring soft-deleted users
  • Bulk deletion with confirmation

Quick Check: What is the advantage of soft delete over hard delete? (Answer: Soft delete allows you to recover data if needed)

Best Practices

8

Safe Deletion Guidelines

# ============================================================
# BEST PRACTICES FOR DELETE
# ============================================================

print("1. ALWAYS USE WHERE CLAUSE")
print("   - Without WHERE, ALL rows are deleted")
print("   - This is the most common and dangerous mistake")

print("\n2. TEST WITH SELECT FIRST")
print("   - Use SELECT with the same WHERE clause")
print("   - Verify you're deleting the right rows")

print("\n3. USE TRANSACTIONS")
print("   - Start a transaction before DELETE")
print("   - Commit only when you're sure")
print("   - Rollback if something goes wrong")

print("\n4. CHECK ROW COUNT")
print("   - Always check cursor.rowcount")
print("   - Verify the expected number of rows deleted")

print("\n5. USE PARAMETERIZED QUERIES")
print("   - Always use %s placeholders")
print("   - Never build queries with string concatenation")
print("   - Protects against SQL injection")

print("\n6. BACKUP BEFORE DELETING")
print("   - For major deletions, backup the table")
print("   - CREATE TABLE backup_students AS SELECT * FROM students")

print("\n7. CONSIDER SOFT DELETE")
print("   - Add an is_deleted column")
print("   - Update instead of delete")
print("   - Allows recovery of data")

print("\n8. LOG YOUR DELETIONS")
print("   - Keep track of who deleted what")
print("   - When and why data was deleted")

print("\n9. USE LIMIT FOR LARGE DELETIONS")
print("   - Delete in small batches")
print("   - Prevents long-running transactions")

print("\n10. GET CONFIRMATION")
print("   - Always confirm before deleting")
print("   - Especially for important data")

Summary of best practices:

  • Always use WHERE β€” never delete all rows unintentionally
  • Test with SELECT β€” verify before you delete
  • Use transactions β€” commit or rollback safely
  • Check rowcount β€” verify how many rows were deleted
  • Parameterized queries β€” prevent SQL injection
  • Backup first β€” for major deletions
  • Soft delete β€” mark instead of removing
  • Log deletions β€” keep track of what was removed
  • Get confirmation β€” double-check before deleting

Quick Check: What is the most important rule for DELETE statements? (Answer: Always include a WHERE clause and test with SELECT first)

Try It Yourself

Experiment with DELETE statements in the editor below.

Loading Pyodide... 0%
Python Code Editor
========================================
DELETE DATA - PRACTICE
========================================

1. SINGLE ROW DELETE
----------------------------------------

Before deletion:
-------------------------------------------------------
ID: 1 | Rahul Sharma | Age: 22 | rahul@email.com
ID: 2 | Priya Patel | Age: 25 | priya@email.com
ID: 3 | Amit Singh | Age: 24 | amit@email.com
ID: 4 | Sneha Reddy | Age: 23 | sneha@email.com
ID: 5 | Vikram Kumar | Age: 26 | vikram@email.com
ID: 6 | Anjali Nair | Age: 21 | anjali@email.com
ID: 7 | Ravi Desai | Age: 27 | ravi@email.com
ID: 8 | Meera Iyer | Age: 23 | meera@email.com
Total: 8 students

πŸ”„ Deleting student with ID 5...
Deleted 1 student(s)

After deletion:
-------------------------------------------------------
ID: 1 | Rahul Sharma | Age: 22 | rahul@email.com
ID: 2 | Priya Patel | Age: 25 | priya@email.com
ID: 3 | Amit Singh | Age: 24 | amit@email.com
ID: 4 | Sneha Reddy | Age: 23 | sneha@email.com
ID: 6 | Anjali Nair | Age: 21 | anjali@email.com
ID: 7 | Ravi Desai | Age: 27 | ravi@email.com
ID: 8 | Meera Iyer | Age: 23 | meera@email.com
Total: 7 students

2. MULTIPLE ROW DELETE
----------------------------------------

πŸ”„ Deleting all students aged 23...
Deleted 2 student(s)

After age deletion:
-------------------------------------------------------
ID: 1 | Rahul Sharma | Age: 22 | rahul@email.com
ID: 2 | Priya Patel | Age: 25 | priya@email.com
ID: 3 | Amit Singh | Age: 24 | amit@email.com
ID: 6 | Anjali Nair | Age: 21 | anjali@email.com
ID: 7 | Ravi Desai | Age: 27 | ravi@email.com
Total: 5 students

3. DELETE BY CONDITION
----------------------------------------

πŸ”„ Deleting all students older than 25...
Deleted 2 student(s)

After condition deletion:
-------------------------------------------------------
ID: 1 | Rahul Sharma | Age: 22 | rahul@email.com
ID: 2 | Priya Patel | Age: 25 | priya@email.com
ID: 3 | Amit Singh | Age: 24 | amit@email.com
ID: 6 | Anjali Nair | Age: 21 | anjali@email.com
Total: 4 students

4. DELETE ALL (with safety)
----------------------------------------

⚠️ Be careful! This deletes ALL remaining students!
❌ Deletion cancelled

βœ… DELETE is powerful! Always use with caution!
πŸ†

You've Got It!

You now know how to safely delete data from MySQL using Python. You understand single row deletion, multiple row deletion, and important safety measures.

Quick Quiz

Test what you've learned:

1. What does the DELETE statement do?
2. What happens if you omit the WHERE clause in a DELETE?
3. What is a soft delete?
4. What is the difference between DELETE and TRUNCATE?
5. What should you do before running a major DELETE?

Frequently Asked Questions

What happens if I forget the WHERE clause in DELETE? β–Ό

If you forget the WHERE clause, ALL rows in the table will be deleted. This is a very common and dangerous mistake. Always double-check your DELETE statements before running them.

Can I undo a DELETE? β–Ό

If you use transactions, you can rollback a DELETE before committing. If you've already committed, you'll need to restore from backup. That's why it's important to backup data before major deletions and use soft delete when possible.

What is the difference between DELETE and DROP? β–Ό

DELETE removes rows (data) from a table but keeps the table structure. DROP removes the entire table (both data AND structure). DROP is permanent and cannot be rolled back.

What is a common interview question about DELETE? β–Ό

Common questions include: "What happens if you run DELETE without WHERE?" "What is the difference between DELETE and TRUNCATE?" and "How would you safely delete data from a production database?"

How can I recover deleted data? β–Ό

If you used a transaction and haven't committed, you can rollback. If you've committed, you'll need to restore from a backup. That's why soft delete (marking as deleted) is often preferred for important data.

Where to Go From Here

Now that you know how to safely delete data, check out these related topics:

Update Data

Learn how to modify existing data.

Learn More β†’

Transactions

Learn how to safely manage database changes.

Learn More β†’

Error Handling

Learn how to handle database errors properly.

Learn More β†’
Interview Resources
  • Python Syntax & Variables Interview Questions
  • Top SQL Interview Questions & Answers
  • SQL Joins: Displaying Data from Multiple Tables FAQ
  • Python Lists and Dictionaries Interview Questions
  • Python OOP Interview Questions
  • SQL Set Operators Interview Questions
Previous: Update Data Next: Parameterized Queries β†’