delete()

The delete() method removes one or more rows from a table based on specified conditions. It supports prepared statements and returns true on success or false on failure.

Signature:

public function delete(string $tableName, int|array|null $numRows = null): bool

Basic Usage

$db->where('id', 42);
$success = $db->delete('test_users');

Delete with LIMIT

You can restrict the number of affected rows using the optional $numRows parameter:

$db->where('status', 'inactive');
$db->delete('test_users', 10); // Delete max 10 users

Return Value

  • true on success
  • false on failure (check getLastError())

Notes

  • Always use a where() condition to avoid deleting all rows unintentionally
  • Prepared statements protect against SQL injection
  • Supports JOIN-based deletes if needed (see advanced examples)
  • The optional $numRows can be an integer or array (e.g. [10, 20])

See Also