FileSystemHandle: remove() method

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.

The remove() method of the FileSystemHandle interface requests removal of the entry represented by the handle from the underlying file system.

The remove() method allows you to remove a file or directory directly from its handle. Without this method, you would have to obtain the handle of the parent directory, then call FileSystemDirectoryHandle.removeEntry() on that to remove it.

You can also call remove() on the root directory of the Origin Private File System to clear its contents, after which a new empty OPFS is created.

Syntax

js
remove()
remove(options)

Parameters

options Optional

An object that specifies options for the removal. Possible properties are as follows:

recursive Optional

A boolean value that defaults to false. When set to true and the entry is a directory, its contents will be removed recursively.

Return value

A Promise that fulfills with a value of undefined.

Exceptions

InvalidModificationError DOMException

Thrown if recursive is set to false and the entry to be removed is a directory with children.

NoModificationAllowedError DOMException

Thrown if the browser was not able to get an exclusive lock on the entry.

NotAllowedError DOMException

Thrown if PermissionStatus is not granted.

NotFoundError DOMException

Thrown if the entry is not found.

Examples

Our FileSystemHandle.remove() demo (see the source code) is a file creator app. You can enter text into the <textarea> and press the "Save file" <button>, and the app will open a file picker allowing you to save that text onto your local file system in a text file of your choice. You can also choose to delete files you create.

It doesn't allow you to view the content of created files, and it doesn't stay in sync with the underlying file system on page reloads/closes. This means that files created by the app will still exist on the file system if you don't choose to delete them before reloading or closing the tab.

The file picker, file handle, and the file itself if you are creating a new file, are created using window.showSaveFilePicker(). The text is written to the file via FileSystemFileHandle.createWritable().

Once a file is created on the file system, an entry is created in the app (see processNewFile() in the source code):

  • A reference to the file handle is stored in an array called savedFileRefs so it can be easily referenced later on.
  • A list item is added underneath the "Saved files" heading in the UI, with the file name shown alongside a "Delete" button.

When the "Delete" button is pressed, the deleteFile() function is run, which looks like so:

js
async function deleteFile(e) {
  for (const handle of savedFileRefs) {
    if (handle.name === `${e.target.id}.txt`) {
      await handle.remove();
      savedFileRefs = savedFileRefs.filter(
        (handle) => handle.name !== `${e.target.id}.txt`,
      );
      e.target.parentElement.parentElement.removeChild(e.target.parentElement);
    }
  }
}

Walking through this:

  1. For each file handle saved in the savedFileRefs array, we check its name to see if it matches the id attribute of the button that triggered the event.
  2. When a match is found, we run FileSystemHandle.remove() on that handle to remove the file from the underlying file system.
  3. We also remove the matched handle from the savedFileRefs array.
  4. Finally, we remove the list item relating to that file in the UI.

Specifications

This feature is not part of any specification, but may become standard in the future. See whatwg/fs#9 for the detail.

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
remove
Experimental Non-standard
Chrome – Full support
Chrome 110 (Release date: 2023-02-07)
footnote Full support
Edge – Full support
Edge 110 (Release date: 2023-02-09)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – Full support
Opera 96 (Release date: 2023-02-22)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 110 (Release date: 2023-02-07)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 74 (Release date: 2023-03-13)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 21 (Release date: 2023-05-19)
footnote Full support
WebView Android – Full support
WebView Android 110 (Release date: 2023-02-07)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
Non-standard. Check cross-browser support before using.

See also