目录

Deleting Data

You can use the delete() method to delete a single object or a group of objects. When deleting data, Cloud DB will delete the corresponding data based on the input object's primary key, without checking whether other attributes of the object are consistent with the stored data. The delete operation is atomic. That is, all objects in the list either are deleted successfully or fail to be deleted.

  • Objects to be deleted in a batch must belong to the same object type. Otherwise, the delete operation will fail.

  • When calling the data deletion method, there are two ways to return the result: either by returning a Promise object or by passing a callback object in the parameters. The following example explains the Promise approach in detail.

Constraints

This feature supports phones and tablets by default. Starting from version 5.1.0(18), support for wearables is added. From version 5.1.1(19), TVs are also supported. From version 6.1.0(23), PCs/2-in-1 devices are supported as well.

Prerequisites

You have initialized database access.

Deleting Data

  1. Import required modules.

    import { cloudDatabase } from '@kit.CloudFoundationKit';
    import { BookInfo } from '../model/BookInfo';
    import { hilog } from '@kit.PerformanceAnalysisKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. When you delete objects, the number of deleted objects will be returned if the deletion succeeds; otherwise, an exception will be thrown.

    let book = new BookInfo();
    book.id = 11;
    databaseZone.delete(book).then((deleteNum: number) => {
      hilog.info(0x0000, 'cloudDb', `Succeeded in deleting: ${deleteNum}`);
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'cloudDb', `Failed to delete, code: ${err.code}, message: ${err.message}`);
    });