Example of how nodeJS deletes a file

Source: Internet
Author: User
This article mainly introduces the method of deleting files in nodeJS, and analyzes the operations related to obtaining, traversing, and deleting files and directories in nodeJS by using examples, for more information about how to delete a node. js file, see the following example. We will share this with you for your reference. The details are as follows:

Var fs = require ("fs"); var path = require ("path"); deleteFolderRecursive = function (url) {var files = []; // determine whether the specified path exists if (fs. existsSync (url) {// array files of returned files and subdirectories = fs. readdirSync (url); files. forEach (function (file, index) {// var curPath = url + "/" + file; var curPath = path. join (url, file); // fs. statSync synchronously reads folder files. if it is a folder, trigger the function if (fs. statSync (curPath ). isDirectory () {// recurse deleteFolderRecursive (curPath); // yes file delete file} else {fs. unlinkSync (curPath) ;}}); // clear the folder fs. rmdirSync (url);} else {console. log ("the specified path does not exist, please provide the correct path") ;}}; deleteFolderRecursive (". /node_modules ");

I hope this article will help you design nodejs programs.

For more articles on nodeJS file deletion method examples, refer to PHP Chinese network!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.