borg prune
**********

   borg [common options] prune [options] [REPOSITORY]


Description
===========

The prune command prunes a repository by deleting all archives not
matching any of the specified retention options.

Important: Repository disk space is **not** freed until you run "borg
compact".

This command is normally used by automated backup scripts wanting to
keep a certain number of historic backups. This retention policy is
commonly referred to as GFS (Grandfather-father-son) backup rotation
scheme.

Also, prune automatically removes checkpoint archives (incomplete
archives left behind by interrupted backup runs) except if the
checkpoint is the latest archive (and thus still needed). Checkpoint
archives are not considered when comparing archive counts against the
retention limits ("--keep-X").

If a prefix is set with -P, then only archives that start with the
prefix are considered for deletion and only those archives count
towards the totals specified by the rules. Otherwise, *all* archives
in the repository are candidates for deletion! There is no automatic
distinction between archives representing different contents. These
need to be distinguished by specifying matching prefixes.

If you have multiple sequences of archives with different data sets
(e.g. from different machines) in one shared repository, use one prune
call per data set that matches only the respective archives using the
-P option.

The "--keep-within" option takes an argument of the form
"<int><char>", where char is "H", "d", "w", "m", "y". For example, "--
keep-within 2d" means to keep all archives that were created within
the past 48 hours. "1m" is taken to mean "31d". The archives kept with
this option do not count towards the totals specified by any other
options.

A good procedure is to thin out more and more the older your backups
get. As an example, "--keep-daily 7" means to keep the latest backup
on each day, up to 7 most recent days with backups (days without
backups do not count). The rules are applied from secondly to yearly,
and backups selected by previous rules do not count towards those of
later rules. The time that each backup starts is used for pruning
purposes. Dates and times are interpreted in the local time zone, and
weeks go from Monday to Sunday. Specifying a negative number of
archives to keep means that there is no limit. As of Borg 1.2.0, Borg
will retain the oldest archive if any of the secondly, minutely,
hourly, daily, weekly, monthly, quarterly, or yearly rules was not
otherwise able to meet its retention target. This enables the first
chronological archive to continue aging until it is replaced by a
newer archive that meets the retention criteria.

The "--keep-13weekly" and "--keep-3monthly" rules are two different
strategies for keeping archives every quarter year.

The "--keep-last N" option is doing the same as "--keep-secondly N"
(and it will keep the last N archives under the assumption that you do
not create more than one backup archive in the same second).

When using "--stats", you will get some statistics about how much data
was deleted - the "Deleted data" deduplicated size there is most
interesting as that is how much your repository will shrink. Please
note that the "All archives" stats refer to the state after pruning.


Examples
========

Be careful: prune is a potentially dangerous command; it will remove
backup archives.

By default, prune applies to **all archives in the repository** unless
you restrict its operation to a subset of the archives using "--glob-
archives". When using "--glob-archives", be careful to choose a good
matching pattern — for example, do not use "foo*" if you do not also
want to match "foobar".

It is strongly recommended to always run "prune -v --list --dry-run
..." first, so you can see what it would do without actually doing
anything.

   # Keep 7 end-of-day and 4 additional end-of-week archives.
   # Do a dry-run without actually deleting anything.
   $ borg prune -v --list --dry-run --keep-daily=7 --keep-weekly=4 /path/to/repo

   # Same as above but only apply to archive names starting with the hostname
   # of the machine followed by a "-" character:
   $ borg prune -v --list --keep-daily=7 --keep-weekly=4 --glob-archives='{hostname}-*' /path/to/repo
   # Actually free disk space:
   $ borg compact /path/to/repo

   # Keep 7 end-of-day, 4 additional end-of-week archives,
   # and an end-of-month archive for every month:
   $ borg prune -v --list --keep-daily=7 --keep-weekly=4 --keep-monthly=-1 /path/to/repo

   # Keep all backups in the last 10 days, 4 additional end-of-week archives,
   # and an end-of-month archive for every month:
   $ borg prune -v --list --keep-within=10d --keep-weekly=4 --keep-monthly=-1 /path/to/repo

There is also a visual example of pruning in "docs/misc/prune-
example.txt":

   borg prune visualized
   =====================

   Assume it is 2016-01-01, today's backup has not yet been made, you have
   created at least one backup on each day in 2015 except on 2015-12-19 (no
   backup made on that day), and you started backing up with borg on
   2015-01-01.

   This is what borg prune --keep-daily 14 --keep-monthly 6 --keep-yearly 1
   would keep.

   Backups kept by the --keep-daily rule are marked by a "d" to the right,
   backups kept by the --keep-monthly rule are marked by a "m" to the right,
   and backups kept by the --keep-yearly rule are marked by a "y" to the
   right.

   Calendar view
   -------------

                               2015
         January               February               March
   Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
             1y 2  3  4                     1                     1
    5  6  7  8  9 10 11   2  3  4  5  6  7  8   2  3  4  5  6  7  8
   12 13 14 15 16 17 18   9 10 11 12 13 14 15   9 10 11 12 13 14 15
   19 20 21 22 23 24 25  16 17 18 19 20 21 22  16 17 18 19 20 21 22
   26 27 28 29 30 31     23 24 25 26 27 28     23 24 25 26 27 28 29
                                               30 31

          April                  May                   June
   Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
          1  2  3  4  5               1  2  3   1  2  3  4  5  6  7
    6  7  8  9 10 11 12   4  5  6  7  8  9 10   8  9 10 11 12 13 14
   13 14 15 16 17 18 19  11 12 13 14 15 16 17  15 16 17 18 19 20 21
   20 21 22 23 24 25 26  18 19 20 21 22 23 24  22 23 24 25 26 27 28
   27 28 29 30           25 26 27 28 29 30 31  29 30m


           July                 August              September
   Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
          1  2  3  4  5                  1  2      1  2  3  4  5  6
    6  7  8  9 10 11 12   3  4  5  6  7  8  9   7  8  9 10 11 12 13
   13 14 15 16 17 18 19  10 11 12 13 14 15 16  14 15 16 17 18 19 20
   20 21 22 23 24 25 26  17 18 19 20 21 22 23  21 22 23 24 25 26 27
   27 28 29 30 31m       24 25 26 27 28 29 30  28 29 30m
                         31m

         October               November              December
   Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su  Mo Tu We Th Fr Sa Su
             1  2  3  4                     1      1  2  3  4  5  6
    5  6  7  8  9 10 11   2  3  4  5  6  7  8   7  8  9 10 11 12 13
   12 13 14 15 16 17 18   9 10 11 12 13 14 15  14 15 16 17d18d19 20d
   19 20 21 22 23 24 25  16 17 18 19 20 21 22  21d22d23d24d25d26d27d
   26 27 28 29 30 31m    23 24 25 26 27 28 29  28d29d30d31d
                         30m

   List view
   ---------

   --keep-daily 14     --keep-monthly 6     --keep-yearly 1
   ----------------------------------------------------------------
    1. 2015-12-31       (2015-12-31 kept     (2015-12-31 kept
    2. 2015-12-30        by daily rule)       by daily rule)
    3. 2015-12-29       1. 2015-11-30        1. 2015-01-01 (oldest)
    4. 2015-12-28       2. 2015-10-31
    5. 2015-12-27       3. 2015-09-30
    6. 2015-12-26       4. 2015-08-31
    7. 2015-12-25       5. 2015-07-31
    8. 2015-12-24       6. 2015-06-30
    9. 2015-12-23
   10. 2015-12-22
   11. 2015-12-21
   12. 2015-12-20
       (no backup made on 2015-12-19)
   13. 2015-12-18
   14. 2015-12-17


   Notes
   -----

   2015-12-31 is kept due to the --keep-daily 14 rule (because it is applied
   first), not due to the --keep-monthly or --keep-yearly rule.

   The --keep-yearly 1 rule does not consider the December 31st backup because it
   has already been kept due to the daily rule. There are no backups available
   from previous years, so the --keep-yearly target of 1 backup is not satisfied.
   Because of this, the 2015-01-01 archive (the oldest archive available) is kept.

   The --keep-monthly 6 rule keeps Nov, Oct, Sep, Aug, Jul and Jun. December is
   not considered for this rule, because that backup was already kept because of
   the daily rule.

   2015-12-17 is kept to satisfy the --keep-daily 14 rule - because no backup was
   made on 2015-12-19. If a backup had been made on that day, it would not keep
   the one from 2015-12-17.

   We did not include weekly, hourly, minutely or secondly rules to keep this
   example simple. They all work in basically the same way.

   The weekly rule is easy to understand roughly, but hard to understand in all
   details. If interested, read "ISO 8601:2000 standard week-based year".

   The 13weekly and 3monthly rules are two different strategies for keeping one
   every quarter of a year. There are `multiple ways` to define a quarter-year;
   borg prune recognizes two:

   * --keep-13weekly keeps one backup every 13 weeks using ISO 8601:2000's
     definition of the week-based year. January 4th is always included in the
     first week of a year, and January 1st to 3rd may be in week 52 or 53 of the
     previous year. Week 53 is also in the fourth quarter of the year.
   * --keep-3monthly keeps one backup every 3 months. January 1st to
     March 31, April 1st to June 30th, July 1st to September 30th, and October 1st
     to December 31st form the quarters.

   If the subtleties of the definition of a quarter year don't matter to you, a
   short summary of behavior is:

   * --keep-13weekly favors keeping backups at the beginning of Jan, Apr, July,
     and Oct.
   * --keep-3monthly favors keeping backups at the end of Dec, Mar, Jun, and Sept.
   * Both strategies will have some overlap in which backups are kept.
   * The differences are negligible unless backups considered for deletion were
     created weekly or more frequently.

   .. _multiple ways: https://en.wikipedia.org/wiki/Calendar_year#Quarter_year
