Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท fs and fs/promises

Node.js fs.truncate() method

Reference for fs.truncate(), fs.ftruncate() and fs.truncateSync() in Node.js: shorten a file or extend it with zero bytes, and empty files without deleting them.

fs.truncate(path[, len], callback) changes the size of a file to exactly len bytes. If the file is longer, the extra bytes are cut off. If it is shorter, it grows and the new part is filled with zero bytes. With no len the default is 0, which empties the file but keeps it (and its permissions and inode) in place.

fs.ftruncate(fd[, len], callback) does the same for an open file descriptor. There are synchronous versions (truncateSync, ftruncateSync) and promise versions (fsPromises.truncate(), filehandle.truncate()).

NODE
import { truncateSync } from 'node:fs';

truncateSync('server.log');   // now 0 bytes

Try it

Node.js Shrinking, emptying and growing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js ftruncate on an open file and error cases

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
fs.truncate(path[, len], callback)
fs.truncateSync(path[, len]): undefined
fs.ftruncate(fd[, len], callback)
fs.ftruncateSync(fd[, len]): undefined

// callback: (err: Error | null) => void
// fsPromises.truncate(path[, len]): Promise<undefined>
// filehandle.truncate(len): Promise<undefined>
Name Description
path The file to resize (string, Buffer or URL).
fd An open, writable file descriptor (for ftruncate).
len New size in bytes. Default 0. Should be a non-negative integer; a negative value currently behaves like 0, but do not rely on that.

Returns: nothing. The callback gets an error or null.

Errors: ENOENT (no such file), EISDIR (a directory), EACCES/EPERM (no write permission), EBADF (descriptor not open for writing), ERR_INVALID_ARG_TYPE for a non-number length.

Usage notes

  • Emptying a file in place: truncate(path) keeps the same file object. Programs that hold the file open (a logger, tail -f) keep working, while deleting and recreating the file would not affect their handle.
  • Growing creates zeros. The extra part is zero bytes and, on many file systems, a sparse region that takes no disk space until written.
  • The file position does not move. After ftruncate() on an open descriptor, the current position may now be beyond the end. The next sequential write fills the gap with zeros.
  • Passing a descriptor to truncate() is deprecated; use ftruncate().
  • path must exist. Unlike writeFile, truncate never creates a file. Use writeFile(path, '') to create or empty.
  • Opening with flag 'w' also truncates, at open time. truncate is for when the file is already open or you only want to shorten it.
  • Cuts bytes, not characters. Truncating a UTF-8 text file in the middle of a multi-byte character leaves invalid text.