Open In App

Node.js fsPromises.truncate() Method

Last Updated : 04 Apr, 2023
Like Article

The fsPromises.truncate() method in node.js is used to change the size of the file i.e. either increase or decrease the file size. This method changes the length of the file at the path by len bytes. If len represents a length shorter than the file’s current length, the file is truncated to that length. If it is greater than the file length is padded by appending null bytes (x00) until len is reached.

It then resolves the Promise with no arguments upon success. The path must be a String or Buffer.


fsPromises.truncate( path, len )

Parameters: This method accepts two parameters as mentioned above and described below:

  • path: It holds the path of the targeted file. It can be either string, a buffer, or a URL.
  • len: It holds the length of the file after which the file will be truncated. It takes an integer input and it is not the mandatory condition as it is default set to 0.

Return Value: This method returns the Promise.

Example: This example illustrates the working of fsPromises.truncate() method in Node.js:

Create a Hello.txt file in the current root directory with some sample text as shown below: 

Greetings from GeeksforGeeks

Filename: index.js 


// Node.js program to demonstrate the
// fsPromises.truncate() method
// Include the fs module
const fs = require('fs');
const fsPromises = fs.promises;
fsPromises.truncate('Hello.txt', 0)
.then(function() {
    console.log("File Content Deleted");
.catch(function(error) {

Step to run this program: Run the index.js file using the following command: 

node index.js


File Content Deleted


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads