Skip to main content

"deleteRecords" (jsonAction)

JSON DB "deleteRecords" action deletes records from a table using its primary key

The "deleteRecords" action deletes records from a database table using its primary key, such as the id field.

  • The action returns the values from the deleted records in case you want to insert them again, view them, or verify the correct records are deleted.

  • If the specified records do not exist, the action returns success because this is the desired end result.

  • The "totalRecordCount" property in the response is the number of records that are deleted by this action.

  • A table must have a primary key to delete records.

    • Tables created by the "createTable" action, automatically have an auto increment "id" field indexed as primary key.

    • Tables created by other APIs may not have an "id" field; instead, they may have a primary key index that includes one or more fields in the table.

    • The "deleteRecords" action provides an optional  "primaryKeys" property that can retrieve records using any primary key index.

    • The "primaryKeys"  and "ids" properties are mutually exclusive.

Request examples

Minimal

{
  "api": "db",
  "action": "deleteRecords",
  "params": {
    "tableName": "test1",
    "ids": [
      1,
      2
    ]
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}
{
  "api": "db",
  "action": "deleteRecords",
  "params":
  {
    "tableName": "pk_example",
    "primaryKeys": 
    [ 
      [
        {
          "fieldName": "first_name",
          "value": "Sam"
        },
        {
          "fieldName": "last_name",
          "value": "I-am"
        }
      ]
    ]
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}
{
  "api": "db",
  "action": "deleteRecords",
  "params":
  {
    "tableName": "pk_example",
    "primaryKeys": 
    [ 
      [
        {
          "fieldName": "first_name",
          "value": "Sam"
        },
        {
          "fieldName": "last_name",
          "value": "I-am"
        }
      ],
      [
        {
          "fieldName": "first_name",
          "value": "The Cat"
        },
        {
          "fieldName": "last_name",
          "value": "in the Hat"
        }
      ]
    ]
  },
  "authToken": "replaceWithAuthTokenFromCreateSession"
}
{
  "api": "db",
  "apiVersion": "1.0",
  "requestId": "4",
  "action": "deleteRecords",
  "params": {
    "databaseName": "ctreeSQL",
    "ownerName": "admin",
    "tableName": "test1",
    "ids": [
      1,
      2
    ]
  },
  "responseOptions": {
    "binaryFormat": "hex",
    "dataFormat": "objects",
    "numberFormat": "string",
    "includeFields": [],
    "excludeFields": []
  },
  "debug": "max",
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

Note

If the target object does not exist, the response returns success and the debug property contains a warning that the object was not found.

{
  "result": {
    "dataFormat": "objects",
    "binaryFormat": "hex",
    "fields": [
      {
        "name": "id",
        "type": "bigint",
        "length": null,
        "scale": null,
        "defaultValue": null,
        "nullable": false,
        "primaryKey": 1,
        "autoValue": "incrementOnInsert"
      },
      {
        "name": "changeId",
        "type": "bigint",
        "length": null,
        "scale": null,
        "defaultValue": null,
        "nullable": true,
        "primaryKey": 0,
        "autoValue": "changeId"
      },
      {
        "name": "name",
        "type": "varchar",
        "length": 50,
        "scale": null,
        "defaultValue": null,
        "nullable": true,
        "primaryKey": 0,
        "autoValue": "none"
      }
    ],
    "data": [],
    "primaryKeyFields": [
      "id"
    ],
    "changeIdField": "changeId",
    "totalRecordCount": 0
  },
  "errorCode": 0,
  "errorMessage": "",
  "authToken": "replaceWithAuthTokenFromCreateSession"
}
{
  "result": {
      "dataFormat": "objects",
      "binaryFormat": "hexadecimal",
      "fields": [
          {
              "name": "id",
              "type": "bigint",
              "length": null,
              "scale": null,
              "autoTimestamp": "none",
              "defaultValue": null,
              "nullable": false
          },
          {
              "name": "changeId",
              "type": "bigint",
              "length": null,
              "scale": null,
              "autoTimestamp": "none",
              "defaultValue": null,
              "nullable": true
          },
          {
              "name": "name",
              "type": "varchar",
              "length": 50,
              "scale": null,
              "autoTimestamp": "none",
              "defaultValue": null,
              "nullable": true
          }
      ],
      "data": [
          {
              "changeId": 22788,
              "id": 2,
              "name": "jane"
          }
      ],
      "totalRecordCount": 1
  },
  "requestId": "4",
  "debugInfo": {
      "request": {
          "api": "db",
          "action": "deleteRecords",
          "params": {
              "databaseName": "ctreeSQL",
              "ownerName": "admin",
              "tableName": "test1",
              "ids": [
                  2
              ]
          },
          "apiVersion": "1.0",
          "requestId": "4",
          "responseOptions": {},
          "debug": "max",
          "authToken": "replaceWithAuthTokenFromCreateSession"
      },
      "serverSuppliedValues": {
          "databaseName": "ctreeSQL",
          "ownerName": "admin"
      },
      "errorData": {
          "errorData": null
      },
      "warnings": []
  },
  "errorCode": 0,
  "errorMessage": "",
  "authToken": "replaceWithAuthTokenFromCreateSession"
}

The jsonAction JSON DB API "deleteRecords" action deletes records from a database table.

API actionsJSON DB APIjsonActiondelete recordsdeleteRecords

The "params" property is an object that contains an action's parameters. Each action defines its own required and optional properties.

Table 1. "params" property summaries

Property

Description

Default

Type

Limits (inclusive)

databaseName

(optional) specifies the name of a database

Defaults to the "defaultDatabaseName" value that is set during "createSession". If no default is set during "createSession", then "faircom" is used.

string

1 to 64 bytes

ids

(optional) specifies a unique identifier for a row in a table. It is mutually exclusive with "primaryKeys".

"null"

It is required when "primaryKeys" is omitted.

array

ownerName

(optional) specifies the unique name of a schema in a database

""

string

1 to 64 bytes

primaryKeys

(optional) specifies key values used by "getRecordsByIds" to find and return one or more records. It is mutually exclusive with "ids".

"null"

It is required when "ids" is omitted.

array of arrays

tableName

specifies the name of a table

Required - No default value

string

1 to 64 bytes



The "databaseName" property is an optional string that specifies the database that contains the tables. It defaults to the database name supplied at login.

Note

In the API Explorer, "defaultDatabaseName" is set to "ctreeSQL" in the "createSession" action that happens at login.

  • A zero-length "databaseName" is invalid.

  • Its limits are from 0 to 64 bytes.

  • If the "databaseName" property is omitted or set to null, the server will use the default database name specified at login.

  • If no default database is specified during "createSession", "defaultDatabaseName" will be set to the "defaultDatabaseName" value that is specified in the services.json file.

The "ids" property is an array that contains a unique identifier for a row in a table. Its default value is "null" but it is required if the "primaryKeys" property is "null" or not specified.

  • The "ids" property is mutually exclusive with the "primaryKeys" property meaning it is required when "primaryKeys" is omitted or an error is returned if both have values.

  • It is typically an array of integers ("ids": [1,3,5]).

  • It can be an array of an array of strings ("ids": ["9555444333222111","9555444333222112", "9555444333222113"]).

    • A string "id" supports numbers larger than 9,007,199,254,740,991.

    • This is the largest number supported by many programming languages and JSON parser implementations that use IEEE double-precision floats to hold numbers.

  • It can be the primary key value of another field in the table making it useful when your table is created by another API, such as SQL, that allows any field in the table to be the primary key.

    If your table does not have an "id" field but uses a "vin" field as the primary key, you can use vin values to look up records ("ids": [ "4Y1SL65848Z411439", "1HGBH41JXMN109186" ]).

  • If your table uses more than one field as the primary key, you must use the "primaryKeys" property to look up records.

    Tip

    The "getRecordsByIds'' action uses a primary key index to look up records. A primary key index must be a unique, non-null index without conditional filtering. For best performance and maximum simplicity, create tables using the JSON DB API because it automatically creates an auto increment "id" field that is indexed as a primary key.

The "ownerName" property is an optional string from 1 to 64 bytes that specifies the account that owns an object.

The "primaryKeys" property is an array of arrays that contain key values used by the "getRecordsByIds" action to find and return one or more records. The default value is "null" but it is required if the "ids" property is omitted.

  • "primaryKeys" is best used if tables were created with primary keys composed of multiple fields. If primary keys are composed of a single field, it is best to use "ids".

    Note

    Tables created using JSON DB API actions cannot create primary keys composed of multiple fields.

  • A table must have a primary key defined in order to use "primaryKeys"

  • The "primaryKeys" property is mutually exclusive with the "ids" property meaning it is required when "ids" is omitted or an error is returned if both have values.

  • The  "primaryKeys" property is an array of arrays

    • The outer array contains one or more primary key definitions which allow the server to retrieve multiple records at a time.

    • Each inner array is a primary key definition that specifies the values the server needs to retrieve one matching record.

    • A primary key definition consists of one or more objects where each object is a field-value pair that uses the following structure ({ "fieldName": "someField","value": "someValue" }).

Example

If your table uses the "first_name" and "last_name" fields as the primary key, the following "primaryKeys" property will retrieve two records.

Note

If your table does not have a primary key, its records cannot be retrieved, updated or deleted using the "getRecordsByIds", "updateRecords" and "deleteRecords" actions. Other getRecords actions can query its records.

   "primaryKeys": 
    [ 
      [
        { "fieldName": "first_name", "value": "Sam" },
        { "fieldName": "last_name",  "value": "I-am" }
      ],
      [
        { "fieldName": "first_name", "value": "The Cat" },
        { "fieldName": "last_name",  "value": "in the Hat" }
      ]
    ]

The "tableName" property is a string containing the name of a table.

  • A table name may contain up to 64 ASCII characters and must not start with a number.

Table 2. "result" property summaries

Property

Description

Type

Limits (inclusive)

binaryFormat

specifies how binary values are returned

string

One of the following: "base64", "hex", or "byteArray".

data

(optional) specifies an array or object that the server returns, such as records returned by a query. It is always included in a response but contains an empty array when no results are available.

array

Its contents are determined by the action

dataFormat

specifies the format of the data in the "data" property

string

"autoDetect"
"arrays"
"objects"

fields

specifies an array of objects set by the server, where each object is the definition of a field in a table defining the details of each field returned by a query

array

totalRecordCount

specifies an integer, the server sets its value to the number of records returned by a query

integer

-1 to 99999999999999999999999999999999



The "data" property contains a response message. Its contents are defined by the action. It is an empty array when no results are available.

Example

arrays
"data":
  [
   ["test1", ".\\test1.dbs\\SQL_SYS", 1003]
  ]
objects
"data":
[
  {
    "databaseName": "test7",
    "path": ".\\test7.dbs\\SQL_SYS",
    "uid": 1015
  }
]

The "dataFormat" property is an optional, case-insensitive string enum that defines the format of the response in the "data" property. The default format is an array of arrays. The alternative is an array of objects. The default for "dataFormat" can be changed during a "createSession" action by assigning a different value to the "dataFormat" property in "defaultResponseOptions".

There are three different (but similar) versions of the "dataFormat" property:

Two of those versions occur in a request and another occurs in a response. They all indicate how data is formatted.

  • "dataFormat" in the request inside "responseOptions" determines how the "data" property in the response is formatted.

    Possible values include:

    • "arrays"

      This is the default and causes the server to return results as an array of arrays, which is the most efficient.

    • "objects"

      This returns results as an array of objects. This is less efficient but is simpler to generate, read, and troubleshoot.

  • "dataFormat" in the request in the "params" object notifies the server how the "sourceData" property is formatted in the request. This version is rarely used because of the default "autoDetect" behavior.

    Possible values include:

    • "arrays"

      This causes the server to return results as an array of arrays, which is the most efficient.

    • "objects"

      This returns results as an array of objects. This is less efficient but is simpler to generate, read, and troubleshoot.

    • "autoDetect"

      This is the default and causes the server to automatically detect the format of the data in the "sourceData" property.

  • "dataFormat" in the response shows the client how the server formatted the "data" property.

    Possible values include:

    • "arrays"

      This is the default and causes the server to return results as an array of arrays, which is the most efficient.

    • "objects"

      This returns results as an array of objects. This is less efficient but is simpler to generate, read, and troubleshoot.

The "fields" property is a required array of field-type objects. There is one object for each top-level field in the array. Each object identifies the actual data type of the field as stored in the database.

A field-type object is used when creating a table. It contains one object for each field definition returned in the data.

Full request example

"fields": 
[
    {
      "name": "name",
      "type": "varchar",
      "length": 50,
      "scale": null,
      "defaultValue": null,
      "nullable": false,
      "primaryKey":1
   }
]

Full response example

"fields":
[
  {
      "name": "id",
      "type": "bigint",
      "length": null,
      "scale": null,
      "autoValue": "none",
      "defaultValue": null,
      "nullable": false,
      "primaryKey": 1
  }
]

The "totalRecordCount" property contains the total available number of records that can be returned from a query.

  • The "totalRecordCount" is set to -1, when the server does not know the total record count.

  • A very fast way to get the total number of records in a table is to call the "getRecordsByTable" method without applying a "tableFilter". This immediately returns the count without reading and counting records.

  • For most methods, the server does not calculate "totalRecordCount" because calculating it requires walking all records in the query, which may take a significant amount of time.

  • When the result is returned as a cursor, "totalRecordCount" is the total number of records that the cursor can traverse.

    Note

    This does not apply to cursor responses.

  • When the result returns records directly, "totalRecordCount" is the total number of records that can be retrieved – not necessarily the number of records returned.

Would you like to provide feedback?