Welcome to this step-by-step guide on deleting multiple records in Xano based on multiple criteria! In this tutorial, we'll walk through an example where we want to delete all projects associated with the marketing department that have a low importance level.
Step 1: Create a New API Endpoint
First, let's create a new API endpoint for our deletion operation:
- Go to the API section in Xano.
- Click on "Create New API" and select "Start from Scratch."
- Name your API endpoint (e.g., "DeleteMarketingLowPriority") and choose the "Delete" method.
Step 2: Define the Criteria Parameters
Next, we'll define the parameters that will determine which records should be deleted:
- In the "Parameters" section, add a new "Table Reference" parameter and select the "Department" table. This will allow us to specify the department we want to filter by.
- Add another parameter of type "Text" and name it "Importance." This will let us specify the importance level we want to filter by (e.g., "Low," "Medium," "High").
Step 3: Query the Matching Records
Before deleting any records, we need to retrieve the ones that match our criteria:
- In the "Database Requests" section, add a new "Query All Records" request for the "Project" table.
- Click on the pencil icon next to "By Custom Query" to set up filters.
- Add a condition where the "project.departmentID" field is equal to the "department" parameter we defined earlier.
- Add another condition with an "AND" operator where the "project.importance" field is equal to the "importance" parameter.
- Give this query a descriptive name (e.g., "MatchingProjectRecords") and save it.
Test the query by providing sample values for the "department" and "importance" parameters to ensure you're getting the expected results.
Step 4: Loop Through and Delete Matching Records
Now we'll set up a loop to iterate through the matching records and delete them one by one:
- In the "Data Manipulation" section, add a new "For Each Loop" that iterates over the "MatchingProjectRecords" list.
- Inside the loop, add a new "Delete Record" request targeting the "Project" table.
- In the "Field Value" section, select the looped "item" variable and append ".ID" to target the record's unique identifier.
By doing this, the loop will go through each matching record and delete it based on its ID.
Step 5: Test and Run
You're all set! Let's test our deletion operation:
- Provide the appropriate values for the "department" and "importance" parameters (e.g., "3" for the marketing department and "Low" for the importance level).
- Run the API endpoint and check for a success message.
- Go to the "Project" table in your database to confirm that the matching records have been deleted.
Congratulations! You've successfully learned how to delete multiple records matching multiple criteria in Xano. This powerful feature can be incredibly useful for maintaining and managing your data efficiently.