Home > Web Front-end > Front-end Q&A > Deleting files from fs in nodejs

Deleting files from fs in nodejs

王林
Release: 2023-05-24 11:36:07
Original
3109 people have browsed it

In Node.js, we can use the fs module to read and write files, which also includes file deletion operations. This article will introduce how to delete files using the fs module of Node.js.

fs module

In Node.js, the fs module is one of the most commonly used modules. It provides basic file operation functions, including file reading, Write, modify, delete and other operations. To use the fs module in Node.js, you first need to import the module:

const fs = require('fs');
Copy after login

fs.unlink()

fs.unlink()Method for deleting files. Its syntax is as follows:

fs.unlink(path, callback)
Copy after login

Among them, path represents the file path to be deleted, which can be an absolute path or a relative path; callback is the callback function after the deletion is completed. .

For example, delete the example.txt file in the current directory:

fs.unlink('example.txt', (err) => {
  if (err) throw err;
  console.log('文件已删除');
});
Copy after login

In the callback function, if an error occurs, an exception will be thrown, otherwise it will be output File deleted.

fs.unlinkSync()

In addition to using the callback function for asynchronous deletion operations, you can also use the fs.unlinkSync() method to perform synchronous deletion operations. How to use it Similar to the fs.unlink() method:

fs.unlinkSync(path);
Copy after login

For example, delete the example.txt file in the current directory:

fs.unlinkSync('example.txt');
Copy after login

But be careful Unfortunately, an exception will be thrown if an error occurs during the synchronous deletion operation, so error handling needs to be performed when calling this method.

Delete directory

Using fs.unlink() and fs.unlinkSync() methods can only delete files. If you want to delete a directory, You can use the fs.rmdir() or fs.rmdirSync() method, as shown below:

fs.rmdir(path, callback);
fs.rmdirSync(path);
Copy after login

where path means to delete The directory path can be an absolute path or a relative path.

It should be noted that when using fs.rmdir() to delete a directory, the directory must be an empty directory, otherwise the deletion will fail. If you want to delete a non-empty directory, you need to delete all files in the directory first, and then delete the directory.

Summary

This article introduces how to delete files using the fs module of Node.js, which are fs.unlink() and fs.unlinkSync(). To delete a directory, use the fs.rmdir() and fs.rmdirSync() methods. When using these methods to delete files, you need to ensure that the files are not used by other programs during the operation, otherwise the deletion will fail.

The above is the detailed content of Deleting files from fs in nodejs. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template