Welcome to Xano's guide on deleting records from your database tables! In this tutorial, we'll explore how to remove individual records as well as multiple records at once using Xano's intuitive no-code interface. Let's dive in!
Deleting a Single Record
To delete a single record from your database table, follow these simple steps:
- Navigate to the Function Stack: Go to the "Function Stack" section in your Xano project, then click on "Database Requests," and finally, select the "Delete Record" function.
- Select the Target Table: Choose the table from which you want to delete a record. In this example, we'll use the "Product" table.
- Specify the Record to Delete: Under the "Inputs" tab, you'll find the "Field Name" and "Field Value" fields. The "Field Name" is typically set to "ID" by default, but you can choose any field or column name from the table to identify the record you want to delete. The "Field Value" is the actual value you want to use to find and delete the record. This can be an input value or a variable from your function stack.
For instance, if you want to delete a product record with a specific ID, you can map that product ID from your function stack to the "Field Value" input.
- Run the Function: After configuring the inputs, click "Run" to execute the delete operation. Xano will remove the specified record from the table.
It's important to note that when you delete a record, it's permanently removed from the database. Additionally, while Xano returns the deleted record in the response, keep in mind that the record no longer exists in the table after deletion.
Deleting Multiple Records
What if you need to delete multiple records at once? Xano has you covered! Here's how you can achieve that:
- Query the Records to Delete: Use the "Query All Records" function from your target table to fetch the list of records you want to delete. You can specify conditions or filters in the "By Custom Query" section to narrow down the records based on specific criteria (e.g., `where product.userID = 3` to find all products belonging to user ID 3).
- Iterate Through the Records: Since you're dealing with a list of records, you'll need to iterate through them using a "For Each Loop" from the "Data Manipulation" section.
- Delete Each Record: Inside the "For Each Loop," drag and drop the "Delete Record" function. In the "Delete Record" function, instead of specifying a fixed "Field Value," you can use the `item` variable from the loop and dot notation to access the ID (or any other field) of the current record (e.g., `item.id`).
- Run the Function: Execute the function, and Xano will iterate through the list of records, deleting each one based on the specified criteria.
By following these steps, you can efficiently remove multiple records from your database table in a single operation.
Remember, deleting records is a permanent action, so exercise caution when executing these functions, especially when dealing with multiple records at once.
With Xano's visual interface and intuitive functions, managing your database records has never been easier. Happy coding (or should we say, happy no-coding)!