1# @system.storage (Data Storage) 2 3> **NOTE** 4> 5> - The APIs of this module are no longer maintained since API version 6, and you are advised to use [`@ohos.data.storage`](js-apis-data-storage.md). From API version 9, you are advised to use [`@ohos.data.preferences`](js-apis-data-preferences.md). 6> 7> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> - The APIs of this module can be used only in the FA model. 10 11## Modules to Import 12 13```js 14import storage from '@system.storage'; 15``` 16 17## storage.get 18 19get(options: GetStorageOptions): void 20 21Reads the value stored in the cache based on the specified key. 22 23**System capability**: SystemCapability.DistributedDataManager.Preferences.Core.Lite 24 25**Parameters** 26 27| Name | Type | Mandatory| Description | 28| ------- | -------------------- | ---- | ---------- | 29| options | [GetStorageOptions](#getstorageoptions) | Yes | API configuration.| 30 31**Example** 32 33```js 34export default { 35 storageGet() { 36 storage.get({ 37 key: 'storage_key', 38 success: function(data) { 39 console.log('call storage.get success: ' + data); 40 }, 41 fail: function(data, code) { 42 console.log('call storage.get fail, code: ' + code + ', data: ' + data); 43 }, 44 complete: function() { 45 console.log('call complete'); 46 }, 47 }); 48 } 49} 50``` 51 52## storage.set 53 54set(options: SetStorageOptions): void 55 56Sets the value in the cache based on the specified key. 57 58**System capability**: SystemCapability.DistributedDataManager.Preferences.Core.Lite 59 60**Parameters** 61 62| Name | Type | Mandatory| Description | 63| ------- | ------------------- | ---- | ---------- | 64| options | [SetStorageOptions](#setstorageoptions) | Yes | API configuration.| 65 66**Example** 67 68```js 69export default { 70 storageSet() { 71 storage.set({ 72 key: 'storage_key', 73 value: 'storage value', 74 success: function() { 75 console.log('call storage.set success.'); 76 }, 77 fail: function(data, code) { 78 console.log('call storage.set fail, code: ' + code + ', data: ' + data); 79 }, 80 }); 81 } 82} 83``` 84 85## storage.clear 86 87clear(options?: ClearStorageOptions): void 88 89Clears the key-value pairs from the cache. 90 91**System capability**: SystemCapability.DistributedDataManager.Preferences.Core.Lite 92 93**Parameters** 94 95| Name | Type | Mandatory| Description | 96| ------- | ------------------------------------------- | ---- | -------------- | 97| options | [ClearStorageOptions](#clearstorageoptions) | No | API configuration.| 98 99**Example** 100 101```js 102export default { 103 storageClear() { 104 storage.clear({ 105 success: function() { 106 console.log('call storage.clear success.'); 107 }, 108 fail: function(data, code) { 109 console.log('call storage.clear fail, code: ' + code + ', data: ' + data); 110 }, 111 }); 112 } 113} 114``` 115 116## storage.delete 117 118delete(options: DeleteStorageOptions): void 119 120Deletes the key-value pair based on the specified key. 121 122**System capability**: SystemCapability.DistributedDataManager.Preferences.Core.Lite 123 124**Parameters** 125 126| Name | Type | Mandatory| Description | 127| ------- | --------------------------------------------- | ---- | -------------- | 128| options | [DeleteStorageOptions](#deletestorageoptions) | Yes | API configuration.| 129 130**Example** 131 132```js 133export default { 134 storageDelete() { 135 storage.delete({ 136 key: 'Storage1', 137 success: function() { 138 console.log('call storage.delete success.'); 139 }, 140 fail: function(data, code) { 141 console.log('call storage.delete fail, code: ' + code + ', data: ' + data); 142 }, 143 }); 144 } 145} 146``` 147 148## GetStorageOptions 149 150**System capability**: SystemCapability.DistributedDataManager.Preferences.Core.Lite 151 152| Name | Type | Mandatory| Description | 153| -------- | ---------------- | ---- | ------------------- | 154| key | string | Yes | Key of the target data. | 155| default | string | No | Default value returned when the specified key does not exist. | 156| success | (data: any) => void | No | Called to return the result when **storage.get()** is called successfully. **data** is the value indexed by the specified key. | 157| fail | (data: string, code: number) => void | No | Called to return the result when **storage.get()** fails to be called. **data** is the error information, and **code** indicates the error code. | 158| complete | () => void | No | Called when **storage.get()** is complete. | 159 160 161## SetStorageOptions 162 163**System capability**: SystemCapability.DistributedDataManager.Preferences.Core.Lite 164 165| Name | Type | Mandatory| Description | 166| -------- | ------------------- | ---- | -------------------- | 167| key | string | Yes | Key of the data to set. | 168| value | string | Yes | New value to set. The length must be less than 128 bytes. | 169| success | () => void | No | Called when **storage.set()** is called successfully. | 170| fail | (data: string, code: number) => void | No | Called to return the result when **storage.set()** fails to be called. **data** is the error information, and **code** indicates the error code. | 171| complete | () => void | No | Called when **storage.set()** is complete. | 172 173 174## ClearStorageOptions 175 176**System capability**: SystemCapability.DistributedDataManager.Preferences.Core.Lite 177 178| Name | Type | Mandatory| Description | 179| -------- | --------------------- | ---- | -------------------- | 180| success | () => void | No | Called when **storage.clear()** is called successfully. | 181| fail | (data: string, code: number) => void | No | Called to return the result when **storage.clear()** fails to be called. **data** is the error information, and **code** indicates the error code. | 182| complete | () => void | No | Called when **storage.clear()** is complete. | 183 184 185## DeleteStorageOptions 186 187**System capability**: SystemCapability.DistributedDataManager.Preferences.Core.Lite 188 189| Name | Type | Mandatory| Description | 190| -------- | -------------------- | ---- | ------------------ | 191| key | string | Yes | Key of the data to delete. | 192| success | () => void | No | Called when **storage.delete()** is called successfully. | 193| fail | (data: string, code: number) => void | No | Called to return the result when **storage.delete()** fails to be called. **data** is the error information, and **code** indicates the error code. | 194| complete | () => void | No | Called when **storage.delete()** is complete. | 195