Deleting Data

Use delete() to remove rows from a table. You can pass a filter as a dictionary or a raw SQL string. Without a filter, all rows in the table are deleted.

db.delete(table, where=None)

Parameters

Parameter Type Description
table str Name of the table
where dict or str Optional filter. Dict keys are ANDed; or pass a raw SQL string.

Delete by ID

# DELETE FROM products WHERE id = 42
db.delete('products', {'id': 42})

Delete with multiple conditions

# DELETE FROM products WHERE category = 'discontinued' AND stock = 0
db.delete('products', {'category': 'discontinued', 'stock': 0})

Delete with a SQL string

# Delete orders older than 1 year
db.delete('orders', "created_at < DATE_SUB(NOW(), INTERVAL 1 YEAR)")

Delete all rows

Omitting where deletes every row in the table.

db.delete('session_tokens')   # clears the entire table

Next step

See the changelog →