The truncate()
method of the FileSystemSyncAccessHandle
interface resizes the file associated with the handle to a specified number of bytes.
Note: In earlier versions of the spec, close()
, flush()
, getSize()
, and truncate()
were wrongly specified as asynchronous methods, and older versions of some browsers implement them in this way. However, all current browsers that support these methods implement them as synchronous methods.
async function truncateFile() {
const root = await navigator.storage.getDirectory();
const draftHandle = await root.getFileHandle("draft.txt", { create: true });
const accessHandle = await draftHandle.createSyncAccessHandle();
accessHandle.truncate(0);
accessHandle.flush();
accessHandle.close();
}