Skip to main content
FileHandle.truncate - fs/promises - Node documentation
method FileHandle.truncate

Usage in Deno

import { type FileHandle } from "node:fs/promises";
FileHandle.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

optional
len: number = 0

Return Type

Promise<void>

Fulfills with undefined upon success.

Back to top