Use flow to delete old backups (files in general) in folder

The question is independant of my nextcloud installation, so I skip the support/intro part.

I have several folders where backups of whattsapp, signal and threema are uploaded to. Thats 2,4 GB per day or files. I would like to keep only the newest 3 backups. Can I realize this with flow?
So everything in the folder but the 3 newest files should be deleted. The deletion or check should be done every day.

How do you want to trigger something daily in Flow? The easiest way would be to start a cron job for such a simple task that needs to be done daily.

The cron job should be run as your nextcloud webserver user (www-data)

This could be the script that does the cleanup (adapt the variables to your need):

# This script should be run once per day as the webserver user (www-data) by cron


# Check if the directory exists
if [ ! -d "$NC_DATADIR/$USER_ID/$PATH" ]; then
    echo "Directory does not exist: $NC_DATADIR/$USER_ID/$PATH"
    exit 1

# Change to the directory
cd "$NC_DATADIR/$USER_ID/$PATH" || exit

# Check if files exist that can be deleted
for pattern in "${PATTERN[@]}"; do
    files_to_delete=($(ls -1t $pattern-*.backup 2>/dev/null))
    if [ ${#files_to_delete[@]} -gt 3 ]; then
        # Delete older files
        echo "Deleting older files for pattern: $pattern"
        echo "${files_to_delete[@]:3}" | xargs rm

# Run the Nextcloud scan command
php -f "$NC_DIR/occ" files:scan --path=$USER_ID/$PATH

That’s pretty much how I would do it but you have to test it yourself thoroughly before you run it as a cron job. I would test it on test files to not make it delete valuable data :wink:

Much and good luck,