Skip to main content

truncate

method promises.FileHandle.truncate
Jump to headingFileHandle.truncate(len?: number): Promise<void>

Truncates the file.

If the file was larger than len bytes, only the first len bytes will be retained in the file.

The following example retains only the first four bytes of the file:

import { open } from 'node:fs/promises';

let filehandle = null;
try {
  filehandle = await open('temp.txt', 'r+');
  await filehandle.truncate(4);
} finally {
  await filehandle?.close();
}

If the file previously was shorter than len bytes, it is extended, and the extended part is filled with null bytes ('\0'):

If len is negative then 0 will be used.

Parameters Jump to heading

optional
Jump to headinglen: number = 0

Return Type Jump to heading

Promise<void>

Fulfills with undefined upon success.

Back to top