-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
29 lines (23 loc) · 777 Bytes
/
script.js
File metadata and controls
29 lines (23 loc) · 777 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const File = require ('./models/file');
const fs = require ('fs');
const connectDB = require("./config/db");
connectDB();
async function fetchData() {
const pastDate = new Date ( Date.now() - (24 * 60 * 60 *1000) ); // 24 hours converted into milliseconds
const files = await File.find({ createdAt: {$lt: pastDate}})
if(files.length) {
for(const file of files) {
try{
fs.unlinkSync(file.path);
await file.remove();
console.log(`successfully deleted ${file.filename}`);
} catch(err){
console.log(`Error while deleting file ${err}`);
}
}
console.log('job done!')
}
}
fetchData().then(() => {
process.exit()
})