QNAP NAS- How to Automatically Delete Files

 

Introduction

 

QNAP’s QTS operating system shines in many areas, but there’s one feature that stands out as a glaring omission- there’s no easy way to automatically delete files by scheduling automated clean-up tasks. Since I use my QNAP NAS as a backup server, I do a lot of automatic transfers from my home server to my NAS. While this works great, after a while the video footage from my camera system fills up the storage space.

It’s a pain to constantly stay aware of the storage space remaining on my NAS. Sometimes I catch the red LED on the front panel of the NAS, sometimes I don’t find out until I log in through the web interface. I needed a more reliable way to stay on top of this since my life is a bit hectic at the moment.

What I needed was something similar to Windows Task Scheduler. It turns out that QTS, being a Linux-based distribution, does, in fact, have such a feature. It’s called cron.

The tutorial will show you how to automatically delete files after x amount of days, based on a recurring schedule.


 

A Quick Explanation

 

So what exactly is cron? Cron is a time-based utility that is commonly found on Unix-based operating systems. It allows you to run various tasks based on the date and/or time.

The basic format for a crontab (short for cron table) looks something like this: 0 0 * * * /etc/init.d/antivirus.sh archive_log. What exactly does this mean? Let’s examine:

  • 0- The first argument is the minute (0-59)
  • 0- The second argument in the hour (0-23)
  • *- The third argument is the day of the month (1-31).
  • *- The fourth argument is the month of the year (1-12).
  • *- The fifth argument is the day of the week (0-6 are valid options, 0 = Sunday, 1 = Monday, 2 = Tuesday, etc).
  • /etc/init.d/antivirus.sh archive_log- This is the command that will be executed.

An asterisk (*) means that the field will be ignored. So in this example, a shell script that archives the antivirus log will be executed at midnight each night.

The cron table will contain all of the cron jobs that have been scheduled to run in the system. Before moving on, in case you’re more familiar with Linux than I am, it’s worth pointing out that QNAP’s implementation of cron is a little different than most Linux distributions. That means you won’t be able to use the crontab -e command.



The Solution

 

First of all, we need to download a QPKG that will give us the tool we’ll need to delete the files. This is because the built-in “find” command cannot use the “exec” command. You can download the Entware package from here. Follow the below slideshow to manually install the qpkg:


Slideshow- Tap or click to view

Next click on Browse. Select the .qpkg file from your download folder.

Now just click the Install button.



This is the command you’ll need to copy and paste into the cron table:

0 11 * * * /opt/bin/find “/share/Security Camera Recordings/” -mtime +60 -exec rm {} \;

You can change the 0 and 11 to whatever you want (as is this will execute at 11:00 a.m.).

For the next part, you’ll need PuTTY. If you don’t have it, download it here. Then follow the steps outlined here:


Slideshow- Tap or click to view

Log in as admin.

Type in: vi /etc/config/crontab  to edit your crontab configuration file.

You won’t be able to edit the file until you press the “i” key on the keyboard.

You are now in “insert” mode and will see the word at the bottom of the screen.

Now, create a new line in the file by pressing “Enter” (it doesn’t matter where). Type in: 0 11 * * * /opt/bin/find “/share/Security Camera Recordings/” -mtime +60 -exec rm {} \;. Of course, you’ll want to replace the path with your folder name. Press Ctrl+C twice. You’ll see this message at the bottom of the screen.

Type in: :wq. Notice the colon. ‘wq’ means write then quit. This saves the crontab file.

Next type in crontab /etc/config/crontab  to reload the crontab configuration.

Finally, type in: /etc/init.d/crond.sh restart  to restart the cron service.

You’ll see two messages letting you know the service has stopped then restarted.







Conclusion

 

The poor documentation that QNAP provides to schedule a script to run is extremely disappointing. This pretty much means you’ll either a) have to devote many hours unreasonably trying to fit the puzzle pieces together, or b) get lucky and find a website that shows you exactly how to do it. After much searching, I’m glad I finally figured out how to automatically delete files in my NAS. Hopefully this post helps someone else!

Related topics:


 

About Adam Bollmeyer

I'm a home technology enthusiast with a penchant for home automation, networking, and computers. My goal is to help others improve their knowledge of how available technology can be used at home.