I have an issue with node fs.unlink
I have a function that writes files to disk storage extracts data from the these files then deletes the files
const parseShpFile = async ({
cpgFile,
dbfFile,
prjFile,
qmdFile,
shpFile,
shxFile,
}) => {
// saving binary files to local machine
const dbfPath = await saveBinaryToLocalMachine(dbfFile);
const shpPath = await saveBinaryToLocalMachine(shpFile);
//reading files from local machine using shapefile
const shpParsedData = await shapefile
.open(shpPath, dbfPath)
.then((src) => src.read())
.then(async (data) => {
await deleteFilesFromLocal([shpPath, dbfPath]);
return data;
})
.catch((e) => {
console.log("Error", e);
});
return shpParsedData;
};
the delete function is
const deleteFilesFromLocal = async (filePaths) => {
for (const filePath of filePaths) {
try {
await fsP.unlink(path.resolve(filePath));
} catch (e) {
console.log("error in deleting file", filePath);
}
}
// log after delete finishes
console.log("DELETE CALLED")
return;
};
the write function is
const saveBinaryToLocalMachine = async (binaryFile) => {
const name = binaryFile.name.split("/")[2];
const binary = await binaryFile.async("nodebuffer");
const writePath = path.resolve(`./db/tmp/${name}`);
try {
await fsP.writeFile(writePath, binary);
// Log after the file is saved
console.log("file saved");
return writePath;
} catch (e) {
console.log(e);
}
};
The issue is when I run this code the files are not removed from the desk until the server restarts or shutdowns.
Before server restart or shutdown
After the server restarted.
Logs
Issue when API called for 2nd Time
The issue was with the shapefile lib This is not mentioned in the lib docs example
But there is a function called src.cancel
that is called when the stream is destroyed
so the code that worked is
const parseShpFile = async ({
cpgFile,
dbfFile,
prjFile,
qmdFile,
shpFile,
shxFile,
}) => {
// saving binary files to local machine
const dbfPath = await saveBinaryToLocalMachine(dbfFile);
const shpPath = await saveBinaryToLocalMachine(shpFile);
//reading files from local machine using shapefile
const src = await shapefile.open(shpPath, dbfPath);
const shpParsedData = await src.read();
await src.cancel(); // <=====
await deleteFilesFromLocal([shpPath, dbfPath]);
return shpParsedData;
};