Skip to main content

Remove Row Function

The remove_row function allows you to delete records from the database based on specific conditions. This is useful when cleaning up old data, removing inactive users, or filtering out irrelevant records.


How to Use the remove_row Function

Basic Usage

Remove a row that matches a specific condition.

$deleteConditions = ["username" => "alice"];

$sdb->remove_row("users", $deleteConditions);

This removes all records where username is "alice".


Removing Multiple Rows

To delete multiple records that match a condition:

$deleteConditions = ["role" => "guest"];

$sdb->remove_row("users", $deleteConditions);

This deletes all users who have the role "guest".


Using Conditions with Operators

You can specify conditions using comparison operators:

$deleteConditions = [
"age" => ["<", 18]
];

$sdb->remove_row("users", $deleteConditions);

This removes all users who are younger than 18.


Using AND & OR Conditions

The function supports complex conditions:

Using AND (All conditions must be met)

$deleteConditions = [
"AND" => [
["status" => "inactive"],
["last_login" => ["<", "2023-01-01"]]
]
];

$sdb->remove_row("users", $deleteConditions);

This removes users who are inactive and last logged in before 2023.


Using OR (At least one condition must be met)

$deleteConditions = [
"OR" => [
["country" => "Unknown"],
["email_verified" => false]
]
];

$sdb->remove_row("users", $deleteConditions);

This deletes users who either have "Unknown" as their country or haven't verified their email.


When to Use remove_row

Use remove_row when:

  • You need to delete outdated or unwanted records.
  • You want to clean up the database by removing inactive or irrelevant entries.
  • You need to apply conditional filtering before deletion.

Do NOT use remove_row when:

  • You need to update records (use update_row instead).
  • You want to fetch records without deleting them (use get_row instead).

Final Thoughts

The remove_row function provides a powerful and flexible way to delete data in SwartzDB. Whether removing one record or multiple rows, this function ensures efficient cleanup while maintaining database integrity.