1# @ohos.data.storage (Lightweight Data Storage)
2
3The **DataStorage** module provides applications with data processing capability and allows applications to perform lightweight data storage and query. Data is stored in key-value (KV) pairs. Keys are of the string type, and values can be of the number, string, or Boolean type.
4
5> **NOTE**
6>
7> -  The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> -  The APIs of this module are no longer maintained since API version 9. You are advised to use [@ohos.data.preferences](js-apis-data-preferences.md).
10
11
12## Modules to Import
13
14```js
15import data_storage from '@ohos.data.storage';
16```
17
18## Constants
19
20**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
21
22| Name            | Type | Readable | Writable | Description                                 |
23| ---------------- | -------- | ---- | ---- | ------------------------------------- |
24| MAX_KEY_LENGTH   | number   | Yes  | No  | Maximum length of a key, which is 80 bytes.    |
25| MAX_VALUE_LENGTH | number   | Yes  | No  | Maximum length of a value, which is 8192 bytes. |
26
27
28## data_storage.getStorageSync
29
30getStorageSync(path: string): Storage
31
32Reads the specified file and loads its data to the **Storage** instance for data operations.
33
34**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
35
36**Parameters**
37
38| Name | Type  | Mandatory | Description                      |
39| ------ | ------ | ---- | -------------------------- |
40| path   | string | Yes  | Path of the target file. |
41
42**Return value**
43
44| Type               | Description                                             |
45| ------------------- | ------------------------------------------------- |
46| [Storage](#storage) | **Storage** instance used for data storage operations. |
47
48**Example**
49
50```js
51import featureAbility from '@ohos.ability.featureAbility';
52
53let path;
54let context = featureAbility.getContext();
55context.getFilesDir().then((filePath) => {
56  path = filePath;
57  console.info("======================>getFilesDirPromise====================>");
58
59  let storage = data_storage.getStorageSync(path + '/mystore');
60  storage.putSync('startup', 'auto');
61  storage.flushSync();
62});
63```
64
65
66## data_storage.getStorage
67
68getStorage(path: string, callback: AsyncCallback<Storage>): void
69
70Reads the specified file and loads its data to the **Storage** instance for data operations. This API uses an asynchronous callback to return the result.
71
72**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
73
74**Parameters**
75
76| Name  | Type                                    | Mandatory | Description                      |
77| -------- | ---------------------------------------- | ---- | -------------------------- |
78| path     | string                                   | Yes  | Path of the target file. |
79| callback | AsyncCallback<[Storage](#storage)> | Yes  | Callback used to return the result.                |
80
81**Example**
82
83```js
84import featureAbility from '@ohos.ability.featureAbility';
85
86let path;
87let context = featureAbility.getContext();
88context.getFilesDir().then((filePath) => {
89  path = filePath;
90  console.info("======================>getFilesDirPromise====================>");
91
92  data_storage.getStorage(path + '/mystore', function (err, storage) {
93    if (err) {
94      console.info("Failed to get the storage. path: " + path + '/mystore');
95      return;
96    }
97    storage.putSync('startup', 'auto');
98    storage.flushSync();
99  })
100});
101```
102
103
104## data_storage.getStorage
105
106getStorage(path: string): Promise<Storage>
107
108Reads the specified file and loads its data to the **Storage** instance for data operations. This API uses a promise to return the result.
109
110**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
111
112**Parameters**
113
114| Name | Type  | Mandatory | Description                      |
115| ------ | ------ | ---- | -------------------------- |
116| path   | string | Yes  | Path of the target file. |
117
118**Return value**
119
120| Type                              | Description                           |
121| ---------------------------------- | ------------------------------- |
122| Promise<[Storage](#storage)> | Promise used to return the result. |
123
124**Example**
125
126```js
127import featureAbility from '@ohos.ability.featureAbility';
128
129let path;
130let context = featureAbility.getContext();
131context.getFilesDir().then((filePath) => {
132  path = filePath;
133  console.info("======================>getFilesDirPromise====================>");
134
135  let getPromise = data_storage.getStorage(path + '/mystore');
136  getPromise.then((storage) => {
137    storage.putSync('startup', 'auto');
138    storage.flushSync();
139  }).catch((err) => {
140    console.info("Failed to get the storage. path: " + path + '/mystore');
141  })
142});
143```
144
145
146## data_storage.deleteStorageSync
147
148deleteStorageSync(path: string): void
149
150Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur.
151
152**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
153
154**Parameters**
155
156| Name | Type  | Mandatory | Description                      |
157| ------ | ------ | ---- | -------------------------- |
158| path   | string | Yes  | Path of the target file. |
159
160**Example**
161
162```js
163import featureAbility from '@ohos.ability.featureAbility';
164
165let path;
166let context = featureAbility.getContext();
167context.getFilesDir().then((filePath) => {
168    path = filePath;
169    console.info("======================>getFilesDirPromise====================>");
170
171    data_storage.deleteStorageSync(path + '/mystore');
172});
173```
174
175## data_storage.deleteStorage
176
177deleteStorage(path: string, callback: AsyncCallback<void>): void
178
179Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the result.
180
181**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
182
183**Parameters**
184
185| Name  | Type                     | Mandatory | Description                      |
186| -------- | ------------------------- | ---- | -------------------------- |
187| path     | string                    | Yes  | Path of the target file. |
188| callback | AsyncCallback<void> | Yes  | Callback used to return the result.                |
189
190**Example**
191
192```js
193import featureAbility from '@ohos.ability.featureAbility';
194
195let path;
196let context = featureAbility.getContext();
197context.getFilesDir().then((filePath) => {
198  path = filePath;
199  console.info("======================>getFilesDirPromise====================>");
200
201  data_storage.deleteStorage(path + '/mystore', function (err) {
202    if (err) {
203      console.info("Failed to delete the storage with err: " + err);
204      return;
205    }
206    console.info("Succeeded in deleting the storage.");
207  })
208});
209```
210
211
212## data_storage.deleteStorage
213
214deleteStorage(path: string): Promise<void>
215
216Deletes the singleton **Storage** instance of a file from the memory, and deletes the specified file, its backup file, and damaged files. After the specified files are deleted, the **Storage** instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the result.
217
218**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
219
220**Parameters**
221
222| Name | Type  | Mandatory | Description                      |
223| ------ | ------ | ---- | -------------------------- |
224| path   | string | Yes  | Path of the target file. |
225
226**Return value**
227
228| Type               | Description                           |
229| ------------------- | ------------------------------- |
230| Promise<void> | Promise used to return the result. |
231
232**Example**
233
234```js
235import featureAbility from '@ohos.ability.featureAbility';
236
237let path;
238let context = featureAbility.getContext();
239context.getFilesDir().then((filePath) => {
240  path = filePath;
241  console.info("======================>getFilesDirPromise====================>");
242
243  let promisedelSt = data_storage.deleteStorage(path + '/mystore');
244  promisedelSt.then(() => {
245    console.info("Succeeded in deleting the storage.");
246  }).catch((err) => {
247    console.info("Failed to delete the storage with err: " + err);
248  })
249});
250```
251
252
253## data_storage.removeStorageFromCacheSync
254
255removeStorageFromCacheSync(path: string): void
256
257Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur.
258
259**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
260
261**Parameters**
262| Name | Type  | Mandatory | Description                      |
263| ------ | ------ | ---- | -------------------------- |
264| path   | string | Yes  | Path of the target file. |
265
266**Example**
267
268```js
269import featureAbility from '@ohos.ability.featureAbility';
270
271let path;
272let context = featureAbility.getContext();
273context.getFilesDir().then((filePath) => {
274    path = filePath;
275    console.info("======================>getFilesDirPromise====================>");
276
277    data_storage.removeStorageFromCacheSync(path + '/mystore');
278});
279```
280
281
282## data_storage.removeStorageFromCache
283
284removeStorageFromCache(path: string, callback: AsyncCallback<void>): void
285
286Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses an asynchronous callback to return the result.
287
288**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
289
290**Parameters**
291
292| Name  | Type                     | Mandatory | Description                      |
293| -------- | ------------------------- | ---- | -------------------------- |
294| path     | string                    | Yes  | Path of the target file. |
295| callback | AsyncCallback<void> | Yes  | Callback used to return the result.                |
296
297**Example**
298
299```js
300import featureAbility from '@ohos.ability.featureAbility';
301
302let path;
303let context = featureAbility.getContext();
304context.getFilesDir().then((filePath) => {
305  path = filePath;
306  console.info("======================>getFilesDirPromise====================>");
307
308  data_storage.removeStorageFromCache(path + '/mystore', function (err) {
309    if (err) {
310      console.info("Failed to remove storage from cache with err: " + err);
311      return;
312    }
313    console.info("Succeeded in removing storage from cache.");
314  })
315});
316```
317
318
319## data_storage.removeStorageFromCache
320
321removeStorageFromCache(path: string): Promise<void>
322
323Removes the singleton **Storage** instance of a file from the cache. The removed instance cannot be used for data operations. Otherwise, data inconsistency will occur. This API uses a promise to return the result.
324
325**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
326
327**Parameters**
328
329| Name | Type  | Mandatory | Description                      |
330| ------ | ------ | ---- | -------------------------- |
331| path   | string | Yes  | Path of the target file. |
332
333**Return value**
334
335| Type               | Description                           |
336| ------------------- | ------------------------------- |
337| Promise<void> | Promise used to return the result. |
338
339**Example**
340
341```js
342import featureAbility from '@ohos.ability.featureAbility';
343
344let path;
345let context = featureAbility.getContext();
346context.getFilesDir().then((filePath) => {
347  path = filePath;
348  console.info("======================>getFilesDirPromise====================>");
349
350  let promiserevSt = data_storage.removeStorageFromCache(path + '/mystore')
351  promiserevSt.then(() => {
352    console.info("Succeeded in removing storage from cache.");
353  }).catch((err) => {
354    console.info("Failed to remove storage from cache with err: " + err);
355  })
356});
357```
358
359## Storage
360
361Provides APIs for obtaining and modifying storage data.
362
363Before calling the following APIs, use [data_storage.getStorage](#data_storagegetstorage) or [data_storage.getStorageSync](#data_storagegetstoragesync) to obtain the **Storage** instance.
364
365### getSync
366
367getSync(key: string, defValue: ValueType): ValueType
368
369Obtains the value corresponding to a key. If the value is null or not of the default value type, **defValue** is returned.
370
371**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
372
373**Parameters**
374
375| Name  | Type                   | Mandatory | Description                                                        |
376| -------- | ----------------------- | ---- | ------------------------------------------------------------ |
377| key      | string                  | Yes  | Key of the data. It cannot be empty.                             |
378| defValue | [ValueType](#valuetype) | Yes  | Default value to be returned if the value of the specified key does not exist. It can be a number, string, or Boolean value. |
379
380**Return value**
381
382| Type     | Description                                                    |
383| --------- | -------------------------------------------------------- |
384| ValueType | Value corresponding to the specified key. If the value is null or not in the default value format, the default value is returned. |
385
386**Example**
387
388```js
389let value = storage.getSync('startup', 'default');
390console.info("The value of startup is " + value);
391```
392
393
394### get
395
396get(key: string, defValue: ValueType, callback: AsyncCallback<ValueType>): void
397
398Obtains the value corresponding to a key. If the value is null or not of the default value type, **defValue** is returned. This API uses an asynchronous callback to return the result.
399
400**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
401
402**Parameters**
403
404| Name  | Type                          | Mandatory | Description                                     |
405| -------- | ------------------------------ | ---- | ----------------------------------------- |
406| key      | string                         | Yes  | Key of the data. It cannot be empty.          |
407| defValue | [ValueType](#valuetype)        | Yes  | Default value to be returned. It can be a number, string, or Boolean value. |
408| callback | AsyncCallback<ValueType> | Yes  | Callback used to return the result.                               |
409
410**Example**
411
412```js
413storage.get('startup', 'default', function(err, value) {
414    if (err) {
415        console.info("Failed to get the value of startup with err: " + err);
416        return;
417      }
418    console.info("The value of startup is " + value);
419})
420```
421
422
423### get
424
425get(key: string, defValue: ValueType): Promise<ValueType>
426
427Obtains the value corresponding to a key. If the value is null or not of the default value type, **defValue** is returned. This API uses a promise to return the result.
428
429**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
430
431**Parameters**
432
433| Name  | Type                   | Mandatory | Description                                     |
434| -------- | ----------------------- | ---- | ----------------------------------------- |
435| key      | string                  | Yes  | Key of the data. It cannot be empty.          |
436| defValue | [ValueType](#valuetype) | Yes  | Default value to be returned. It can be a number, string, or Boolean value. |
437
438**Return value**
439
440| Type                    | Description                           |
441| ------------------------ | ------------------------------- |
442| Promise<ValueType> | Promise used to return the result. |
443
444**Example**
445
446```js
447let promiseget = storage.get('startup', 'default');
448promiseget.then((value) => {
449    console.info("The value of startup is " + value)
450}).catch((err) => {
451    console.info("Failed to get the value of startup with err: " + err);
452})
453```
454
455
456### putSync
457
458putSync(key: string, value: ValueType): void
459
460Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**.
461
462**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
463
464**Parameters**
465
466| Name | Type                   | Mandatory | Description                                     |
467| ------ | ----------------------- | ---- | ----------------------------------------- |
468| key    | string                  | Yes  | Key of the data. It cannot be empty.            |
469| value  | [ValueType](#valuetype) | Yes  | New value to store. It can be a number, string, or Boolean value. |
470
471**Example**
472
473```js
474storage.putSync('startup', 'auto');
475```
476
477
478### put
479
480put(key: string, value: ValueType, callback: AsyncCallback<void>): void
481
482Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**. This API uses an asynchronous callback to return the result.
483
484**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
485
486**Parameters**
487
488| Name  | Type                     | Mandatory | Description                                     |
489| -------- | ------------------------- | ---- | ----------------------------------------- |
490| key      | string                    | Yes  | Key of the data. It cannot be empty.            |
491| value    | [ValueType](#valuetype)   | Yes  | New value to store. It can be a number, string, or Boolean value. |
492| callback | AsyncCallback<void> | Yes  | Callback used to return the result.                               |
493
494**Example**
495
496```js
497storage.put('startup', 'auto', function (err) {
498    if (err) {
499        console.info("Failed to put the value of startup with err: " + err);
500        return;
501    }
502    console.info("Succeeded in putting the value of startup.");
503})
504```
505
506
507### put
508
509put(key: string, value: ValueType): Promise<void>
510
511Obtains the **Storage** instance corresponding to the specified file, writes data to the **Storage** instance using a **Storage** API, and saves the modification using **flush()** or **flushSync()**. This API uses a promise to return the result.
512
513**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
514
515**Parameters**
516
517| Name | Type                   | Mandatory | Description                                     |
518| ------ | ----------------------- | ---- | ----------------------------------------- |
519| key    | string                  | Yes  | Key of the data. It cannot be empty.            |
520| value  | [ValueType](#valuetype) | Yes  | New value to store. It can be a number, string, or Boolean value. |
521
522**Return value**
523
524| Type               | Description                       |
525| ------------------- | --------------------------- |
526| Promise<void> | Promise used to return the result. |
527
528**Example**
529
530```js
531let promiseput = storage.put('startup', 'auto');
532promiseput.then(() => {
533    console.info("Succeeded in putting the value of startup.");
534}).catch((err) => {
535    console.info("Failed to put the value of startup with err: " + err);
536})
537```
538
539
540### hasSync
541
542hasSync(key: string): boolean
543
544Checks whether the storage object contains data with a given key.
545
546**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
547
548**Parameters**
549
550| Name | Type  | Mandatory | Description                           |
551| ------ | ------ | ---- | ------------------------------- |
552| key    | string | Yes  | Key of the data. It cannot be empty. |
553
554**Return value**
555
556| Type   | Description                                 |
557| ------- | ------------------------------------- |
558| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise. |
559
560**Example**
561
562```js
563let isExist = storage.hasSync('startup');
564if (isExist) {
565    console.info("The key of startup is contained.");
566}
567```
568
569
570### has
571
572has(key: string, callback: AsyncCallback<boolean>): boolean
573
574Checks whether the storage object contains data with a given key. This API uses an asynchronous callback to return the result.
575
576**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
577
578**Parameters**
579
580| Name  | Type                        | Mandatory | Description                           |
581| -------- | ---------------------------- | ---- | ------------------------------- |
582| key      | string                       | Yes  | Key of the data. It cannot be empty. |
583| callback | AsyncCallback<boolean> | Yes  | Callback used to return the result.                     |
584
585**Return value**
586
587| Type   | Description                           |
588| ------- | ------------------------------- |
589| boolean | Returns **true** if the storage object contains data with the specified key; returns **false** otherwise. |
590
591**Example**
592
593```js
594storage.has('startup', function (err, isExist) {
595    if (err) {
596        console.info("Failed to check the key of startup with err: " + err);
597        return;
598    }
599    if (isExist) {
600        console.info("The key of startup is contained.");
601    }
602})
603```
604
605
606### has
607
608has(key: string): Promise<boolean>
609
610Checks whether the storage object contains data with a given key. This API uses a promise to return the result.
611
612**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
613
614**Parameters**
615
616| Name | Type  | Mandatory | Description                           |
617| ------ | ------ | ---- | ------------------------------- |
618| key    | string | Yes  | Key of the data. It cannot be empty. |
619
620**Return value**
621
622| Type                  | Description                       |
623| ---------------------- | --------------------------- |
624| Promise<boolean> | Promise used to return the result. |
625
626**Example**
627
628```js
629let promisehas = storage.has('startup')
630promisehas.then((isExist) => {
631    if (isExist) {
632        console.info("The key of startup is contained.");
633    }
634}).catch((err) => {
635    console.info("Failed to check the key of startup with err: " + err);
636})
637```
638
639
640### deleteSync
641
642deleteSync(key: string): void
643
644Deletes data with the specified key from this storage object.
645
646**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
647
648**Parameters**
649
650| Name | Type  | Mandatory | Description                             |
651| ------ | ------ | ---- | --------------------------------- |
652| key    | string | Yes  | Key of the data. It cannot be empty. |
653
654**Example**
655
656```js
657 storage.deleteSync('startup');
658```
659
660
661### delete
662
663delete(key: string, callback: AsyncCallback<void>): void
664
665Deletes data with the specified key from this storage object. This API uses an asynchronous callback to return the result.
666
667**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
668
669**Parameters**
670
671| Name  | Type                     | Mandatory | Description                           |
672| -------- | ------------------------- | ---- | ------------------------------- |
673| key      | string                    | Yes  | Key of the data. It cannot be empty. |
674| callback | AsyncCallback<void> | Yes  | Callback used to return the result.                     |
675
676**Example**
677
678```js
679storage.delete('startup', function (err) {
680    if (err) {
681        console.info("Failed to delete startup key failed err: " + err);
682        return;
683    }
684    console.info("Succeeded in deleting startup key.");
685})
686```
687
688
689### delete
690
691delete(key: string): Promise<void>
692
693Deletes data with the specified key from this storage object. This API uses a promise to return the result.
694
695**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
696
697**Parameters**
698
699| Name | Type  | Mandatory | Description                 |
700| ------ | ------ | ---- | --------------------- |
701| key    | string | Yes  | Key of the data. |
702
703**Return value**
704
705| Type               | Description                       |
706| ------------------- | --------------------------- |
707| Promise<void> | Promise used to return the result. |
708
709**Example**
710
711```js
712let promisedel = storage.delete('startup')
713promisedel.then(() => {
714    console.info("Succeeded in deleting startup key.");
715}).catch((err) => {
716    console.info("Failed to delete startup key failed err: " + err);
717})
718```
719
720
721### flushSync
722
723flushSync(): void
724
725Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file.
726
727**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
728
729**Example**
730
731```js
732storage.flushSync();
733```
734
735
736### flush
737
738flush(callback: AsyncCallback<void>): void
739
740Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file. This API uses an asynchronous callback to return the result.
741
742**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
743
744**Parameters**
745
746| Name  | Type                     | Mandatory | Description      |
747| -------- | ------------------------- | ---- | ---------- |
748| callback | AsyncCallback<void> | Yes  | Callback used to return the result. |
749
750**Example**
751
752```js
753storage.flush(function (err) {
754    if (err) {
755        console.info("Failed to flush to file with err: " + err);
756        return;
757    }
758    console.info("Succeeded in flushing to file.");
759})
760```
761
762
763### flush
764
765flush(): Promise<void>
766
767Saves the modification of this object to the **Storage** instance and synchronizes the modification to the file. This API uses a promise to return the result.
768
769**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
770
771**Return value**
772
773| Type               | Description                       |
774| ------------------- | --------------------------- |
775| Promise<void> | Promise used to return the result. |
776
777**Example**
778
779```js
780let promiseflush = storage.flush();
781promiseflush.then(() => {
782    console.info("Succeeded in flushing to file.");
783}).catch((err) => {
784    console.info("Failed to flush to file with err: " + err);
785})
786```
787
788
789### clearSync
790
791clearSync(): void
792
793Clears this **Storage** object.
794
795**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
796
797**Example**
798
799```js
800storage.clearSync();
801```
802
803
804### clear
805
806clear(callback: AsyncCallback<void>): void
807
808Clears this **Storage** object. This API uses an asynchronous callback to return the result.
809
810**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
811
812**Parameters**
813
814| Name  | Type                     | Mandatory | Description      |
815| -------- | ------------------------- | ---- | ---------- |
816| callback | AsyncCallback<void> | Yes  | Callback used to return the result. |
817
818**Example**
819
820```js
821storage.clear(function (err) {
822    if (err) {
823        console.info("Failed to clear the storage with err: " + err);
824        return;
825    }
826    console.info("Succeeded in clearing the storage.");
827})
828```
829
830
831### clear
832
833clear(): Promise<void>
834
835Clears this **Storage** object. This API uses a promise to return the result.
836
837**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
838
839**Return value**
840| Type               | Description                       |
841| ------------------- | --------------------------- |
842| Promise<void> | Promise used to return the result. |
843
844**Example**
845
846```js
847let promiseclear = storage.clear();
848promiseclear.then(() => {
849    console.info("Succeeded in clearing the storage.");
850}).catch((err) => {
851    console.info("Failed to clear the storage with err: " + err);
852})
853```
854
855
856### on('change')
857
858on(type: 'change', callback: Callback<StorageObserver>): void
859
860Subscribes to data changes. The **StorageObserver** needs to be implemented. When the value of the key subscribed to is changed, a callback will be invoked after **flush()** or **flushSync()** is executed.
861
862**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
863
864**Parameters**
865
866| Name  | Type                                               |  Mandatory| Description                                    |
867| -------- | --------------------------------------------------- | ------ |---------------------------------------- |
868| type     | string                                              |Yes| Event type. The value **change** indicates data change events. |
869| callback | Callback<[StorageObserver](#storageobserver)> | Yes|Callback used to return the data change.                          |
870
871**Example**
872
873```js
874let observer = function (key) {
875    console.info("The key of " + key + " changed.");
876}
877storage.on('change', observer);
878storage.putSync('startup', 'auto');
879storage.flushSync();  // observer will be called.
880```
881
882
883### off('change')
884
885off(type: 'change', callback: Callback<StorageObserver>): void
886
887Unsubscribes from data changes.
888
889**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
890
891**Parameters**
892
893| Name  | Type                                               | Mandatory |  Description                                |
894| -------- | --------------------------------------------------- | ------ |---------------------------------------- |
895| type     | string                                              |Yes| Event type. The value **change** indicates data change events. |
896| callback | Callback<[StorageObserver](#storageobserver)> | Yes|Callback for the data change.                |
897
898**Example**
899
900```js
901let observer = function (key) {
902    console.info("The key of " + key + " changed.");
903}
904storage.off('change', observer);
905```
906
907
908## StorageObserver
909
910**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
911
912| Name | Type | Mandatory | Description            |
913| ---- | -------- | ---- | ---------------- |
914| key  | string   | Yes  | Data changed. |
915
916## ValueType
917
918type ValueType = number | string | boolean
919
920Enumerates the value types.
921
922**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
923
924| Type   | Description                |
925| ------- | -------------------- |
926| number  | The value is a number.  |
927| string  | The value is a string.  |
928| boolean | The value is of Boolean type. |
929