-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
35 lines (23 loc) · 811 Bytes
/
script.js
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
30
31
32
33
34
35
const File = require("./models/file");
const fs = require('fs');
const connectDb = require('./config/database');
connectDb();
async function deleteData() {
const pastDate = new Date(Date.now() - (24*60*60*1000));
// Fetch all the files from db
const files = await File.find({ createdAt: {$lt: pastDate} });
// Delete those files which hour 24 or more hours old
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("Deletion complete");
}
}
deleteData().then(process.exit);