We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesApplication ServicesCloud Foundation KitCloud DBDeleting Data

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.

NOTE
  • 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

Sample code:

When you delete objects, the number of deleted objects will be returned if the deletion succeeds; otherwise, an exception will be thrown.

Collapse
Word wrap
Dark theme
Copy code
  1. // If a book is lost, the librarian needs to delete the book from the **BookInfo** table.
  2. async delete() {
  3. try {
  4. let book = new BookInfo();
  5. book.id = 3;
  6. let deleteNum = await databaseZone.delete(book);
  7. hilog.info(0x0000, 'testTag', `Succeeded in deleting data, result: ${JSON.stringify(deleteNum)}`);
  8. } catch (err) {
  9. hilog.error(0x0000, 'testTag', `Failed to delete data, code: ${err.code}, message: ${err.message}`);
  10. }
  11. }
Search in Guides
Enter a keyword.