Related Articles

Related Articles

Node.js | fsPromises.truncate() Method
  • Last Updated : 22 Jun, 2020

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 accept two parameters as mentioned above and described below:

  • path: It holds the path of targeted file. It can be either string, 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 methos returns the Promise.

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

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

Greetings from GeeksforGeeks

Filename: index.js





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


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

node index.js


File Content Deleted


My Personal Notes arrow_drop_up
Recommended Articles
Page :