files/en-us/web/api/filesystemsyncaccesshandle/truncate/index.md
{{securecontext_header}}{{APIRef("File System API")}}{{AvailableInWorkers("dedicated")}}
The truncate() method of the
{{domxref("FileSystemSyncAccessHandle")}} interface resizes the file associated with the handle to a specified number of bytes.
[!NOTE] In earlier versions of the spec, {{domxref("FileSystemSyncAccessHandle.close()", "close()")}}, {{domxref("FileSystemSyncAccessHandle.flush()", "flush()")}}, {{domxref("FileSystemSyncAccessHandle.getSize()", "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.
truncate(newSize)
newSize
None ({{jsxref('undefined')}}).
InvalidStateError {{domxref("DOMException")}}
newSize is larger than the original size of the file, and exceeds the browser's storage quota.async function truncateFile() {
// Get handle to draft file
const root = await navigator.storage.getDirectory();
const draftHandle = await root.getFileHandle("draft.txt", { create: true });
// Get sync access handle
const accessHandle = await draftHandle.createSyncAccessHandle();
// Truncate the file to 0 bytes
accessHandle.truncate(0);
// Persist changes to disk.
accessHandle.flush();
// Always close FileSystemSyncAccessHandle if done.
accessHandle.close();
}
{{Specifications}}
{{Compat}}