首页 > 解决方案 > Node.js completely remove file in a running script

问题描述

I have a Node.js script that is running in a while (true) fashion and watches directories for certain files and removes them using unlink API:

fs.unlinkSync(path);

The files are not created by Node process, but as POSIX documentation about unlink says, files are not removed from disk until process is closed, thus lsof will list them as (deleted) and tied to node process.

My script is some kind of a "watchdog", so how would I go to removing those files and cleaning up disk space without restarting the Node process itself?

Thanks!

标签: node.jsfilefilesystemsposix

解决方案


我会尝试child_process

const { execSync } = require('child_process');
execSync(`rm ${filePath}`)

推荐阅读