Skip to content

I needed this in order to remove files (video and image clips) automatically that were being saved from our home security cameras onto the NAS.

Process

Create the scripts folder

In the DSM open the Control Center > Shared Folder and create a folder named scripts if it does not already exist. Give your account and admin permissions.

Enable SSH (temporary)

In the Control Center go to Terminal and SNMP and enable SSH.

SSH to the Synology

In the terminal:

Once logged in:

sudo su -

Create the script

Change the name to suit you. I'm using a folder named Files as an example.

vi /volume1/scripts/purge_files.sh

#!/bin/bash

# Set the base directory for your Files folder
BASE_DIR="/volume1/Files"

# Find and delete files older than 90 days
find "$BASE_DIR" -type f -mtime +90 -exec rm {} \;

# Find and delete empty directories
find "$BASE_DIR" -type d -empty -delete

echo "Purge complete."

Then you need to set permissions. I'm sure there's a better command for this, but I didn't want to dig around.

chmod 755 /volume1/scripts/purge_files.sh

Access Task Scheduler

  1. Log in to DSM.
  2. Open Control Panel > Task Scheduler

Create a New Task

  1. Click Create > Scheduled Task > User-defined Script.
  2. Give the task a name (e.g., "Purge Files").
    • Run as User: Select a user with sufficient permissions to access and delete files in the Files directory.
  3. Task Settings:

  4. In the Run Command field, input the full path to the script: /bin/bash /volume1/scripts/purge_files.sh >> /volume1/scripts/purge_files.log 2>&1 Note: this will create a log in the scripts directory of all actions taken.

  5. Schedule:

  6. Set the desired schedule, e.g., Daily at 2:00 AM.

Test the Task

  1. Select the task in the Task Scheduler list and click Run to test it.
  2. Check the Files folder to confirm that files older than 90 days are deleted and empty folders are removed.