Forget using today's date rather than date of latest snapshot?

restic 0.19.1 compiled with go1.26.5 on darwin/arm64

Hi,

I have a use case where I want to keep all snapshots with a particular tag for N days after they were made, but after that I don’t care about them at all. They can all be removed; however I know that restic won’t remove all snapshots, so keeping “the latest that is older than N days” is fine. Assuming a daily run of restic forget, is there any restic retention policy that would accomplish this, or do I need something like a Python script to explicitly delete snapshots by ID?

Here is the use case, in case that helps. I make three snapshots a day of my home directory, using a scheduler (launchd on macOS). These are tagged with plan:full. However, from time to time, I do some intensive processing of photographs using apps like Photoshop. During this intensive period, I want to make more frequent snapshots if needed. This would let me “roll back” to an earlier version of my work in case I don’t like my changes. So, what I do is run a daemon script that runs fswatch on my photograph work directories. If fswatch detects a change within a latency period (I have it set to 20 minutes), the script fires a restic command that backs up just my work directories, using a special tag plan:demand.

Think of it as something like git for my photographic work, but unlike git, I only need a short history, say 5 days. If I’m satisfied with my processing decisions for 5 days, it’s highly unlikely that I will need to revisit them. Meanwhile, the daily full plan has snapshots of the image, so I would like to delete all snapshots with the tag plan:demand that are older than 5 days. I don’t want to clutter the repo with tons of snapshots (restic seems to slow down on prune and check operations quite a bit when there are hundreds of snapshots). Plus, the many intermediate snapshots add unnecessary storage space to the repo.

The problem I’m facing is that restic forget --keep* options all use the latest snapshot as the referent, not today’s date. So if I use restic forget --keep-within 5d, it won’t do what I want.

I’m familiar with Python scripting, so I could write a Python script that issues restic forget commands with explicit snapshot IDs, but I want to make sure that’s actually needed.

Thank you.

Hi! We don’t have such a feature, but there’s a PR for it (#5193).

Thanks, @bazinga.

In case anyone needs the functionality, here is a simple Python-3.15 script that will list all snapshots older than --window days (default 7).

#!/usr/bin/env python3
# List all restic snapshots (given on stdin in JSON format) that are older than some number of days

import argparse
import json
import sys
from datetime import date, datetime


def getargs() -> argparse.Namespace:
    def positive_int(value: str) -> int:
        try:
            i = int(value)
        except ValueError:
            raise argparse.ArgumentTypeError(f'Not an integer: {value!r}')
        if i <= 0:
            raise argparse.ArgumentTypeError(f'Must be a positive integer (> 0): {value!r}')
        return i

    parser = argparse.ArgumentParser()
    parser.add_argument('--window', type=positive_int, default=7,
                        help='list snapshots older than WINDOW days (positive integer)')
    return parser.parse_args()


if __name__ == '__main__':
    args = getargs()

    today: date = date.today()

    for line in sys.stdin:
        snapshot_list = json.loads(line)

        for snapshot in snapshot_list:
            snapshot_date: date = datetime.fromisoformat(snapshot['time']).date()
            elapsed_days: int = (today - snapshot_date).days
            if elapsed_days > args.window:
                print(snapshot['short_id'])

I use it like this:

restic snapshots --tag 'plan:demand' --json | ./oldsnaps.py --window=5

which will list all snapshots in the repo older than 5 days (the repo location and password are set by environment variables).

To purge all snapshots older than 5 days, I use it like this (I have shown the command with --dry-run so that copying and pasting doesn’t affect your repo).

restic forget --dry-run $(restic snapshots --tag 'plan:demand' --json | ./oldsnaps.py --window=5)

I hope this might help someone who has a similar requirement.