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:
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.
Access Task Scheduler
- Log in to DSM.
- Open Control Panel > Task Scheduler
Create a New Task
- Click Create > Scheduled Task > User-defined Script.
- 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.
- Run as User: Select a user with sufficient permissions to access and delete files in the
-
Task Settings:
-
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. -
Schedule:
- Set the desired schedule, e.g., Daily at 2:00 AM.
Test the Task
- Select the task in the Task Scheduler list and click Run to test it.
- Check the
Files
folder to confirm that files older than 90 days are deleted and empty folders are removed.