2

I have a react + node website in which I have the following route to delete a folder called cache and remake it. Basically it should "clear cache".

const deleteCache = async (req, res) => {
    fs.rmSync('./cache', {recursive: true, force : true}, (err) => {
        if (err) {
            return res.status(400).json({error: err.message})
        }
    })

    fs.mkdirSync('./cache')
    return res.status(200).send("cache cleared")
    
}

This works great when I test it in postman, but when I connect my frontend code to it as such:

await axios.delete('/api/localfileRoute/deleteCache')
.catch((e) => console.log(e.message))

I get the following error:

/api/localfileRoute/deleteCache undefined
node:fs:1227
  return handleErrorFromBinding(ctx);
         ^

Error: ENOTEMPTY: directory not empty, rmdir '\\?\C:\Users\work\myrepo\server\cache\TD70_captures'
    at Object.rmdirSync (node:fs:1227:10)
    at _rmdirSync (node:internal/fs/rimraf:260:21)
    at rimrafSync (node:internal/fs/rimraf:193:7)
    at node:internal/fs/rimraf:253:9
    at Array.forEach (<anonymous>)
    at _rmdirSync (node:internal/fs/rimraf:250:7)
    at rimrafSync (node:internal/fs/rimraf:193:7)
    at Object.rmSync (node:fs:1276:10)
    at deleteCache (C:\Users\work\myrepo\server\controllers\localfileController.js:85:8)
    at Layer.handle [as handle_request] (C:\Users\work\myrepo\server\node_modules\express\lib\router\layer.js:95:5) {
  errno: -4051,
  syscall: 'rmdir',
  code: 'ENOTEMPTY',
  path: '\\\\?\\C:\\Users\\work\\myrepo\\server\\cache\\TD70_captures'
}

Node.js v18.16.0

When I check the cache folder after this, I see that the nested folders are cleared of their files, leaving me with empty folders and all the files in the cache folder. So recursive stopped after deleting the files inside the nested folder.

I'm not sure why this happens, since the route url is correct and it works in Postman, its just when it's connected to the frontend that it shows me this error. Any tips on how to fix this?

Ruo
  • 77
  • 7

0 Answers0