1# Deleting a Key (ArkTS)
2
3To ensure data security, delete the key that is no longer required.
4
5## How to Develop
6
7For example, delete a 256-bit HKDF key.
8
91. Set the key alias (**keyAlias**), which cannot exceed 128 bytes.
10
112. Initialize the key property set. You can specify [HuksTag](../../reference/apis-universal-keystore-kit/js-apis-huks.md#hukstag) of the key to delete. To delete a single key, leave this parameter empty.
12
133. Use [deleteKeyItem](../../reference/apis-universal-keystore-kit/js-apis-huks.md#huksdeletekeyitem9) to delete the key.
14
15```ts
16/*
17 * Delete a 256-bit HKDF key. This example uses promise-based APIs.
18 */
19import { huks } from '@kit.UniversalKeystoreKit';
20
21/* 1. Set the key alias. */
22let keyAlias = "test_Key";
23/* 2. Construct an empty object. */
24let huksOptions: huks.HuksOptions = {
25  properties: []
26}
27
28class throwObject {
29  isThrow = false;
30}
31
32function deleteKeyItem(keyAlias: string, huksOptions: huks.HuksOptions, throwObject: throwObject) {
33  return new Promise<void>((resolve, reject) => {
34    try {
35      huks.deleteKeyItem(keyAlias, huksOptions, (error, data) => {
36        if (error) {
37          reject(error);
38        } else {
39          resolve(data);
40        }
41      });
42    } catch (error) {
43      throwObject.isThrow = true;
44      throw (error as Error);
45    }
46  });
47}
48
49/* 3. Delete the key. */
50async function publicDeleteKeyFunc(keyAlias: string, huksOptions: huks.HuksOptions) {
51  console.info(`enter promise deleteKeyItem`);
52  let throwObject: throwObject = { isThrow: false };
53  try {
54    await deleteKeyItem(keyAlias, huksOptions, throwObject)
55      .then((data) => {
56        console.info(`promise: deleteKeyItem key success, data = ${JSON.stringify(data)}`);
57      })
58      .catch((error: Error) => {
59        if (throwObject.isThrow) {
60          throw (error as Error);
61        } else {
62          console.error(`promise: deleteKeyItem failed, ${JSON.stringify(error)}`);
63        }
64      });
65  } catch (error) {
66    console.error(`promise: deleteKeyItem input arg invalid, ${JSON.stringify(error)}`);
67  }
68}
69
70async function testDerive() {
71  await publicDeleteKeyFunc(keyAlias, huksOptions);
72}
73```
74