• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.trash (Trash) (System API)
2<!--Kit: Core File Kit-->
3<!--Subsystem: FileManagement-->
4<!--Owner: @wang_zhangjun; @zhuangzhuang-->
5<!--Designer: @wang_zhangjun; @zhuangzhuang; @renguang1116-->
6<!--Tester: @liuhonggang123; @yue-ye2; @juxiaopang-->
7<!--Adviser: @foryourself-->
8
9The **file.trash** module provides APIs for querying, recovering, or permanently deleting the files or directories in **Recently deleted** (trash). Currently, only local files and directories are supported.
10
11You can use **delete()** of [@ohos.file.fileAccess](js-apis-fileAccess-sys.md) to move a file or directory to the trash.
12
13>**NOTE**
14>
15> - The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
16> - The APIs provided by this module are system APIs.
17> - Currently, the APIs of this module can be called only by **FileManager**.
18
19## Modules to Import
20
21```js
22import trash from '@ohos.file.trash';
23```
24
25## trash.listFile
26
27listFile(): Array\<FileInfo>
28
29Lists the files and directories in the **Recently deleted** list.
30
31**Model restriction**: This API can be used only in the stage model.
32
33**System capability**: SystemCapability.FileManagement.UserFileService
34
35**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
36
37**System API**: This is a system API.
38
39**Return value**
40
41  | Type| Description|
42  | --- | -- |
43  | Array [\<FileInfo>](#fileinfo) | List of the files and directories obtained.|
44
45**Error codes**
46
47For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
48
49**Example**
50
51  ```js
52  let fileinfos = trash.listFile();
53  for(let i = 0; i < fileinfos.length; i++){
54    console.info('uri: ' + fileinfos[i].uri);
55    console.info('srcPath: ' + fileinfos[i].srcPath);
56    console.info('fileName: ' + fileinfos[i].fileName);
57    console.info('mode: ' + fileinfos[i].mode);
58    console.info('size: ' + fileinfos[i].size);
59    console.info('mtime: ' + fileinfos[i].mtime);
60    console.info('ctime: ' + fileinfos[i].ctime);
61  }
62  ```
63
64## trash.recover
65
66recover(uri: string): void
67
68Recovers a file or directory from the trash.
69
70**Model restriction**: This API can be used only in the stage model.
71
72**System capability**: SystemCapability.FileManagement.UserFileService
73
74**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
75
76**System API**: This is a system API.
77
78**Parameters**
79
80| Name| Type  | Mandatory| Description                      |
81| ------ | ------ | ---- | -------------------------- |
82| uri   | string | Yes  | URI of the file or directory to recover.|
83
84**Error codes**
85
86For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
87
88**Example**
89
90  ```js
91  let fileinfos = trash.listFile();
92  let uri = fileinfos[0].uri;
93  trash.recover(uri);
94  ```
95
96## trash.completelyDelete
97
98completelyDelete(uri: string): void
99
100Permanently deletes a file or directory from the **Recently deleted** list.
101
102**Model restriction**: This API can be used only in the stage model.
103
104**System capability**: SystemCapability.FileManagement.UserFileService
105
106**Required permissions**: ohos.permission.FILE_ACCESS_MANAGER
107
108**System API**: This is a system API.
109
110**Parameters**
111
112| Name| Type  | Mandatory| Description                      |
113| ------ | ------ | ---- | -------------------------- |
114| uri   | string | Yes  | URI of the file or directory to delete.|
115
116**Error codes**
117
118For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
119
120**Example**
121
122  ```js
123  let fileinfos = trash.listFile();
124  let uri = fileinfos[0].uri;
125  trash.completelyDelete(uri);
126  ```
127
128## FileInfo
129
130Represents information about a file or directory in the **Recently deleted** list.
131
132**Model restriction**: This API can be used only in the stage model.
133
134**System capability**: SystemCapability.FileManagement.UserFileService
135
136| Name| Type  | Read-Only| Optional| Description    |
137| ------ | ------ | -------- | ------ | -------- |
138| uri | string | Yes| No| URI of the file or directory.|
139| srcPath | string | Yes| No| Path of the file or directory before being deleted.|
140| fileName | string | Yes| No| Name of the file or directory.|
141| mode | number | Yes| No| Permission on the file or directory.|
142| size | number | Yes| No|  Size of a file or directory, in bytes.|
143| mtime | number | Yes| No|  Time when the file or directory was last modified. It is the number of milliseconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).|
144| ctime | number | Yes| No|  Time when the file or directory was created. It is the number of seconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).|
145