I have some cctv camera at home. I want to save the snapshots of this in a folder. I know how to get the snapshots using the http request node and also saving it with the file node. But I want a solution in which I have to save only 10 snapshots all together and for the 11 th snapshot, the oldest one of the present should be erased and 11th should be written. To sum up, a simple system of file rotation after 10 snapshots.
If you add the date/time to the file name, you could use the watch node to watch the folder and have it kick off a flow that use one of the contrib file nodes to read the names of all the files. Sort the filenames and delete the oldest ones.
You can quite easily achieve this with the fs-ops nodes.
e.g...

[{"id":"5852e56b.c99cdc","type":"fs-ops-dir","z":"64ce1d00.d049e4","name":"","path":"path","pathType":"msg","filter":"filter","filterType":"msg","dir":"payload","dirType":"msg","x":420,"y":380,"wires":[["665f3ceb.8c1154","b0e4b02c.a9ad6"]]},{"id":"a0ea50a3.1d08a","type":"inject","z":"64ce1d00.d049e4","name":"Settings *edit me*","props":[{"p":"topic","vt":"str"},{"p":"payload"},{"p":"path","v":"c:\\temp","vt":"str"},{"p":"filter","v":"*.gif","vt":"str"},{"p":"maxFiles","v":"10","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":230,"y":380,"wires":[["5852e56b.c99cdc"]]},{"id":"665f3ceb.8c1154","type":"switch","z":"64ce1d00.d049e4","name":"","property":"payload.length","propertyType":"msg","rules":[{"t":"gt","v":"maxFiles","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":590,"y":380,"wires":[["87817f25.12f18"]]},{"id":"87817f25.12f18","type":"split","z":"64ce1d00.d049e4","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":270,"y":460,"wires":[["f0c8a260.914bd"]]},{"id":"f0c8a260.914bd","type":"fs-ops-stats","z":"64ce1d00.d049e4","name":"","path":"path","pathType":"msg","filename":"payload","filenameType":"msg","stats":"stats","sizeType":"msg","x":420,"y":460,"wires":[["114617a6.a53008"]]},{"id":"b0e4b02c.a9ad6","type":"debug","z":"64ce1d00.d049e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":320,"wires":[]},{"id":"1b356211.f6c7fe","type":"join","z":"64ce1d00.d049e4","name":"","mode":"custom","build":"array","property":"stats","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":270,"y":540,"wires":[["c91ee933.68e098"]]},{"id":"25df5110.393d4e","type":"debug","z":"64ce1d00.d049e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":600,"wires":[]},{"id":"114617a6.a53008","type":"change","z":"64ce1d00.d049e4","name":"","rules":[{"t":"set","p":"stats.name","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"stats.path","pt":"msg","to":"path","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":460,"wires":[["1b356211.f6c7fe"]]},{"id":"c91ee933.68e098","type":"function","z":"64ce1d00.d049e4","name":"sort and return oldest","func":"\nvar sorted = msg.stats.sort(function (a, b) {\n var keyA = new Date(a.ctime),\n keyB = new Date(b.ctime);\n // Compare the 2 dates\n if (keyA < keyB) return -1;\n if (keyA > keyB) return 1;\n return 0;\n});\n\nmsg.filesToDelete = sorted.slice(msg.maxFiles);\nmsg.payload = msg.filesToDelete.map(e => e.name);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":460,"y":540,"wires":[["25df5110.393d4e","58457f23.580cd"]]},{"id":"58457f23.580cd","type":"fs-ops-delete","z":"64ce1d00.d049e4","name":"","path":"path","pathType":"msg","filename":"payload","filenameType":"msg","x":670,"y":540,"wires":[[]]}]
↑ adjust the values in the inject to suit ↓ (the rest will just work)

I found that this was removing the newest file than youngest, maybe due to some mixing in configuration. But I was able to achieve the requirement with some slight modification.
[{"id":"5852e56b.c99cdc","type":"fs-ops-dir","z":"51045e07.bbf87","name":"","path":"path","pathType":"msg","filter":"filter","filterType":"msg","dir":"payload","dirType":"msg","x":1580,"y":1680,"wires":[["665f3ceb.8c1154"]]},{"id":"a0ea50a3.1d08a","type":"inject","z":"51045e07.bbf87","name":"Settings *edit me*","props":[{"p":"topic","vt":"str"},{"p":"payload"},{"p":"path","v":"c:\temp","vt":"str"},{"p":"filter","v":"*.gif","vt":"str"},{"p":"maxFiles","v":"10","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1390,"y":1680,"wires":[["5852e56b.c99cdc"]]},{"id":"665f3ceb.8c1154","type":"switch","z":"51045e07.bbf87","name":"","property":"payload.length","propertyType":"msg","rules":[{"t":"gt","v":"maxFiles","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":1710,"y":1680,"wires":[["b50cdf3f.9918a"]]},{"id":"5835602b.2519f","type":"fs-ops-delete","z":"51045e07.bbf87","name":"","path":"path","pathType":"msg","filename":"payload","filenameType":"msg","x":1990,"y":1680,"wires":[[]]},{"id":"b50cdf3f.9918a","type":"function","z":"51045e07.bbf87","name":"","func":"var x = msg.payload[1]\nmsg.payload = x\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1840,"y":1680,"wires":[["5835602b.2519f","5852e56b.c99cdc"]]}]
- your version does not check the created timestamp & might delete random files.
- Also, if the deletion fails you will end up with an infinite loop.
- Also, you are getting the full DIR but only deleteing 1 file at a time then looping back and getting the full directory again (BEFORE the deletion!) - it is likely you will hit an issue where the DIR listing contains a file that has been deleted & cause errors.
Here is a slightly modified version - please check again the files if says it will delete.
[{"id":"94f5e917.e6f958","type":"fs-ops-dir","z":"30e2e9e0.eeaab6","name":"","path":"path","pathType":"msg","filter":"filter","filterType":"msg","dir":"payload","dirType":"msg","x":480,"y":1180,"wires":[["794a07cc.866058","79c18d1.b5b7774"]]},{"id":"84075b2.9ce6da8","type":"inject","z":"30e2e9e0.eeaab6","name":"Settings *edit me*","props":[{"p":"topic","vt":"str"},{"p":"payload"},{"p":"path","v":"c:\\temp","vt":"str"},{"p":"filter","v":"*.gif","vt":"str"},{"p":"maxFiles","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":290,"y":1180,"wires":[["94f5e917.e6f958"]]},{"id":"794a07cc.866058","type":"switch","z":"30e2e9e0.eeaab6","name":"","property":"payload.length","propertyType":"msg","rules":[{"t":"gt","v":"maxFiles","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":650,"y":1180,"wires":[["1413b4cb.1fe89b"]]},{"id":"1413b4cb.1fe89b","type":"split","z":"30e2e9e0.eeaab6","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":330,"y":1260,"wires":[["7bd1322d.959dcc"]]},{"id":"7bd1322d.959dcc","type":"fs-ops-stats","z":"30e2e9e0.eeaab6","name":"","path":"path","pathType":"msg","filename":"payload","filenameType":"msg","stats":"stats","sizeType":"msg","x":480,"y":1260,"wires":[["825e1062.9069"]]},{"id":"79c18d1.b5b7774","type":"debug","z":"30e2e9e0.eeaab6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":1120,"wires":[]},{"id":"e298bf3d.c8bf","type":"join","z":"30e2e9e0.eeaab6","name":"","mode":"custom","build":"array","property":"stats","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":330,"y":1340,"wires":[["320f5a2.5f118a6"]]},{"id":"51cf956a.44b0dc","type":"debug","z":"30e2e9e0.eeaab6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":1400,"wires":[]},{"id":"825e1062.9069","type":"change","z":"30e2e9e0.eeaab6","name":"","rules":[{"t":"set","p":"stats.name","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"stats.path","pt":"msg","to":"path","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":1260,"wires":[["e298bf3d.c8bf"]]},{"id":"320f5a2.5f118a6","type":"function","z":"30e2e9e0.eeaab6","name":"sort and return oldest","func":"\nvar sorted = msg.stats.sort(function (a, b) {\n if (a.ctimeMs < b.ctimeMs) return -1;\n if (a.ctimeMs > b.ctimeMs) return 1;\n return 0;\n});\ndebugger\nmsg.filesToDelete = sorted.slice(msg.maxFiles);\nmsg.payload = msg.filesToDelete.map(e => e.name);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":1340,"wires":[["51cf956a.44b0dc","9de21e39.9b946"]]},{"id":"9de21e39.9b946","type":"fs-ops-delete","z":"30e2e9e0.eeaab6","name":"","path":"path","pathType":"msg","filename":"payload","filenameType":"msg","x":730,"y":1340,"wires":[[]]}]
I checked the updated version. But still it is deleting the oldest files
{"topic":"","payload":["2020-12-09_19-55-11.jpg","2020-12-09_19-55-15.jpg"],"path":"/config/www/snapshots/Detection/BellCam","filter":"*.jpg","maxFiles":10,"_msgid":"71e4b139.2109d","stats":[{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264796,"size":70609,"blocks":144,"atimeMs":1607519730363.0261,"mtimeMs":1607519730363.0261,"ctimeMs":1607519730363.0261,"birthtimeMs":1607519730363.0261,"atime":"2020-12-09T13:15:30.363Z","mtime":"2020-12-09T13:15:30.363Z","ctime":"2020-12-09T13:15:30.363Z","birthtime":"2020-12-09T13:15:30.363Z","name":"2020-12-09_18-45-30.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264669,"size":70609,"blocks":144,"atimeMs":1607519742362.7708,"mtimeMs":1607519742362.7708,"ctimeMs":1607519742362.7708,"birthtimeMs":1607519742362.7708,"atime":"2020-12-09T13:15:42.363Z","mtime":"2020-12-09T13:15:42.363Z","ctime":"2020-12-09T13:15:42.363Z","birthtime":"2020-12-09T13:15:42.363Z","name":"2020-12-09_18-45-42.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":265607,"size":70609,"blocks":144,"atimeMs":1607519743452.7476,"mtimeMs":1607519743462.7473,"ctimeMs":1607519743462.7473,"birthtimeMs":1607519743452.7476,"atime":"2020-12-09T13:15:43.453Z","mtime":"2020-12-09T13:15:43.463Z","ctime":"2020-12-09T13:15:43.463Z","birthtime":"2020-12-09T13:15:43.453Z","name":"2020-12-09_18-45-43.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":266038,"size":70609,"blocks":144,"atimeMs":1607519744612.7227,"mtimeMs":1607519744612.7227,"ctimeMs":1607519744612.7227,"birthtimeMs":1607519744612.7227,"atime":"2020-12-09T13:15:44.613Z","mtime":"2020-12-09T13:15:44.613Z","ctime":"2020-12-09T13:15:44.613Z","birthtime":"2020-12-09T13:15:44.613Z","name":"2020-12-09_18-45-44.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":267480,"size":70609,"blocks":144,"atimeMs":1607519745952.6943,"mtimeMs":1607519745952.6943,"ctimeMs":1607519745952.6943,"birthtimeMs":1607519745952.6943,"atime":"2020-12-09T13:15:45.953Z","mtime":"2020-12-09T13:15:45.953Z","ctime":"2020-12-09T13:15:45.953Z","birthtime":"2020-12-09T13:15:45.953Z","name":"2020-12-09_18-45-45.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264797,"size":73466,"blocks":144,"atimeMs":1607519755222.497,"mtimeMs":1607519755222.497,"ctimeMs":1607519755222.497,"birthtimeMs":1607519755222.497,"atime":"2020-12-09T13:15:55.222Z","mtime":"2020-12-09T13:15:55.222Z","ctime":"2020-12-09T13:15:55.222Z","birthtime":"2020-12-09T13:15:55.222Z","name":"2020-12-09_18-45-55.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264798,"size":71944,"blocks":144,"atimeMs":1607519973277.874,"mtimeMs":1607519973277.874,"ctimeMs":1607519973277.874,"birthtimeMs":1607519973277.874,"atime":"2020-12-09T13:19:33.278Z","mtime":"2020-12-09T13:19:33.278Z","ctime":"2020-12-09T13:19:33.278Z","birthtime":"2020-12-09T13:19:33.278Z","name":"2020-12-09_18-49-33.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264649,"size":73184,"blocks":144,"atimeMs":1607521766908.116,"mtimeMs":1607521766908.116,"ctimeMs":1607521766908.116,"birthtimeMs":1607521766908.116,"atime":"2020-12-09T13:49:26.908Z","mtime":"2020-12-09T13:49:26.908Z","ctime":"2020-12-09T13:49:26.908Z","birthtime":"2020-12-09T13:49:26.908Z","name":"2020-12-09_19-19-26.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264793,"size":73184,"blocks":144,"atimeMs":1607521808087.2312,"mtimeMs":1607521808087.2312,"ctimeMs":1607521808087.2312,"birthtimeMs":1607521808087.2312,"atime":"2020-12-09T13:50:08.087Z","mtime":"2020-12-09T13:50:08.087Z","ctime":"2020-12-09T13:50:08.087Z","birthtime":"2020-12-09T13:50:08.087Z","name":"2020-12-09_19-20-08.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264646,"size":74128,"blocks":152,"atimeMs":1607522483593.1348,"mtimeMs":1607522483593.1348,"ctimeMs":1607522483593.1348,"birthtimeMs":1607522483593.1348,"atime":"2020-12-09T14:01:23.593Z","mtime":"2020-12-09T14:01:23.593Z","ctime":"2020-12-09T14:01:23.593Z","birthtime":"2020-12-09T14:01:23.593Z","name":"2020-12-09_19-31-23.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264647,"size":74128,"blocks":152,"atimeMs":1607523911683.8289,"mtimeMs":1607523911683.8289,"ctimeMs":1607523911683.8289,"birthtimeMs":1607523911683.8289,"atime":"2020-12-09T14:25:11.684Z","mtime":"2020-12-09T14:25:11.684Z","ctime":"2020-12-09T14:25:11.684Z","birthtime":"2020-12-09T14:25:11.684Z","name":"2020-12-09_19-55-11.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":265622,"size":74128,"blocks":152,"atimeMs":1607523915703.7446,"mtimeMs":1607523915703.7446,"ctimeMs":1607523915703.7446,"birthtimeMs":1607523915703.7446,"atime":"2020-12-09T14:25:15.704Z","mtime":"2020-12-09T14:25:15.704Z","ctime":"2020-12-09T14:25:15.704Z","birthtime":"2020-12-09T14:25:15.704Z","name":"2020-12-09_19-55-15.jpg","path":"/config/www/snapshots/Detection/BellCam"}],"filesToDelete":[{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":264647,"size":74128,"blocks":152,"atimeMs":1607523911683.8289,"mtimeMs":1607523911683.8289,"ctimeMs":1607523911683.8289,"birthtimeMs":1607523911683.8289,"atime":"2020-12-09T14:25:11.684Z","mtime":"2020-12-09T14:25:11.684Z","ctime":"2020-12-09T14:25:11.684Z","birthtime":"2020-12-09T14:25:11.684Z","name":"2020-12-09_19-55-11.jpg","path":"/config/www/snapshots/Detection/BellCam"},{"dev":2050,"mode":33188,"nlink":1,"uid":0,"gid":0,"rdev":0,"blksize":4096,"ino":265622,"size":74128,"blocks":152,"atimeMs":1607523915703.7446,"mtimeMs":1607523915703.7446,"ctimeMs":1607523915703.7446,"birthtimeMs":1607523915703.7446,"atime":"2020-12-09T14:25:15.704Z","mtime":"2020-12-09T14:25:15.704Z","ctime":"2020-12-09T14:25:15.704Z","birthtime":"2020-12-09T14:25:15.704Z","name":"2020-12-09_19-55-15.jpg","path":"/config/www/snapshots/Detection/BellCam"}]}
try this...
(note i deleted the last wire to the delete node - restore this once you are happy with what you see in the debug panel)
[{"id":"94f5e917.e6f958","type":"fs-ops-dir","z":"30e2e9e0.eeaab6","name":"","path":"path","pathType":"msg","filter":"filter","filterType":"msg","dir":"payload","dirType":"msg","x":1520,"y":460,"wires":[["794a07cc.866058","79c18d1.b5b7774"]]},{"id":"84075b2.9ce6da8","type":"inject","z":"30e2e9e0.eeaab6","name":"Settings *edit me*","props":[{"p":"topic","vt":"str"},{"p":"payload"},{"p":"path","v":"c:\\temp","vt":"str"},{"p":"filter","v":"*.gif","vt":"str"},{"p":"maxFiles","v":"5","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1330,"y":460,"wires":[["94f5e917.e6f958"]]},{"id":"794a07cc.866058","type":"switch","z":"30e2e9e0.eeaab6","name":"","property":"payload.length","propertyType":"msg","rules":[{"t":"gt","v":"maxFiles","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":1690,"y":460,"wires":[["1413b4cb.1fe89b"]]},{"id":"1413b4cb.1fe89b","type":"split","z":"30e2e9e0.eeaab6","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1370,"y":540,"wires":[["7bd1322d.959dcc"]]},{"id":"7bd1322d.959dcc","type":"fs-ops-stats","z":"30e2e9e0.eeaab6","name":"","path":"path","pathType":"msg","filename":"payload","filenameType":"msg","stats":"stats","sizeType":"msg","x":1520,"y":540,"wires":[["825e1062.9069"]]},{"id":"79c18d1.b5b7774","type":"debug","z":"30e2e9e0.eeaab6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1710,"y":400,"wires":[]},{"id":"e298bf3d.c8bf","type":"join","z":"30e2e9e0.eeaab6","name":"","mode":"custom","build":"array","property":"stats","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1370,"y":620,"wires":[["320f5a2.5f118a6"]]},{"id":"51cf956a.44b0dc","type":"debug","z":"30e2e9e0.eeaab6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1750,"y":680,"wires":[]},{"id":"825e1062.9069","type":"change","z":"30e2e9e0.eeaab6","name":"","rules":[{"t":"set","p":"stats.name","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"stats.path","pt":"msg","to":"path","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1720,"y":540,"wires":[["e298bf3d.c8bf"]]},{"id":"320f5a2.5f118a6","type":"function","z":"30e2e9e0.eeaab6","name":"sort and return oldest","func":"\nvar sorted = msg.stats.sort(function (a, b) {\n if (a.ctimeMs > b.ctimeMs) return -1;\n if (a.ctimeMs < b.ctimeMs) return 1;\n return 0;\n});\n\nmsg.filesToDelete = sorted.slice(msg.maxFiles);\nmsg.payload = msg.filesToDelete.map(e => e.name);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1560,"y":620,"wires":[["51cf956a.44b0dc"]]},{"id":"9de21e39.9b946","type":"fs-ops-delete","z":"30e2e9e0.eeaab6","name":"","path":"path","pathType":"msg","filename":"payload","filenameType":"msg","x":1770,"y":620,"wires":[[]]}]
The fix was to revers the comparison operators in the function.
Yes it worked...thanks

