Database

Initialize the client with your API key

Deletes records from the specified WorqDB table based on the provided where conditions. This endpoint allows you to remove existing records from your database by specifying which records to delete.

DELETE https://api.worqhat.com/db/delete

What Does This Endpoint Do?

This endpoint removes records from your database tables that match specific criteria. Think of it like removing rows from a spreadsheet based on certain conditions - you specify which table to delete from and what conditions the records must meet to be deleted.

When to Use Delete Data

You'll find this endpoint useful when you need to:

  • Remove inactive users: Delete user accounts that are no longer active
  • Clean up old data: Remove outdated or expired records
  • Delete completed tasks: Remove tasks that have been finished
  • Implement data retention policies: Delete data that's beyond your retention period
  • Remove test data: Clean up test or temporary records
  • Handle user requests: Delete user data upon request (e.g., for privacy compliance)

How It Works

  1. You specify the table name where you want to delete records
  2. You provide the where conditions to identify which records to delete
  3. The API deletes matching records and returns the count of deleted records

Code Examples

Example 1: Delete Records with a Single Condition

This example shows how to delete records that match a single condition.

JavaScript

delete-records.js

import Worqhat from 'worqhat';// Initialize the client with your API keyconst client = new Worqhat({apiKey: process.env.WORQHAT_API_KEY, // Always use environment variables for API keys});async function deleteInactiveUsers() {try {  // Call the deleteRecords method  const response = await client.db.deleteRecords({    table: 'users',          // The table to delete from    where: {                 // The condition to match records      status: 'inactive'    },    environment: process.env.WORQHAT_ENVIRONMENT || 'production' // Defaults to production  });    // Handle the successful response  console.log(`Deleted ${response.deletedCount} inactive users`);  console.log(`Message: ${response.message}`);  return response;} catch (error) {  // Handle any errors  console.error('Error deleting users:', error.message);}}// Call the functiondeleteInactiveUsers();

Example 2: Delete Records with Multiple Conditions

This example shows how to delete records that match multiple conditions.

JavaScript

delete-multiple-conditions.js

import Worqhat from 'worqhat';// Initialize the client with your API keyconst client = new Worqhat({apiKey: process.env.WORQHAT_API_KEY,});async function deleteOldCompletedTasks() {try {  // Get date from 30 days ago  const thirtyDaysAgo = new Date();  thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);    // Call the deleteRecords method with multiple conditions  const response = await client.db.deleteRecords({    table: 'tasks',         // The table to delete from    where: {                // Multiple conditions to match records      status: 'completed',      priority: 'low'    },    environment: process.env.WORQHAT_ENVIRONMENT || 'production' // Defaults to production  });    // Handle the successful response  console.log(`Deleted ${response.deletedCount} old completed tasks`);  return response;} catch (error) {  console.error('Error deleting tasks:', error.message);}}deleteOldCompletedTasks();

Request Body Explained

tablestringbodyrequired

The name of the table where you want to delete records. For example, users, products, or orders.

whereobjectbodyrequired

The conditions that records must match to be deleted. This is an object where each key is a field name and each value is the value to match.

For example, {"status": "inactive"} will match records where the status field equals "inactive".

Response Fields Explained

successbooleanrequired

true if the delete operation was successful, false otherwise.

deletedCountintegerrequired

The number of records that were deleted by this operation.

messagestringrequired

A human-readable message describing the result of the operation.

Example Response

response.json

{"success": true,"deletedCount": 5,"message": "5 record(s) deleted successfully from users"}

Common Errors and How to Fix Them

ErrorCauseSolution
"Table not found"The specified table doesn't existCheck your table name for typos
"Missing where conditions"The where parameter is emptyProvide at least one condition in the where object
"No records match the conditions"No records matched your where conditionsThis is not an error, but check your conditions if you expected records to be deleted
"Unauthorized"Invalid or missing API keyCheck that you're using a valid API key

Tips for Successful Deletions

  • Always use specific conditions to avoid accidentally deleting too many records
  • Consider using a query first to verify which records will be deleted
  • Be careful with delete operations as they cannot be undone
  • Use multiple conditions when needed to precisely target records
  • Consider soft deletes (updating a status field instead of actually deleting) for data that might need to be recovered