14

How to delete (with CLI tmutil delete) all old backups from TimeMachine. I wish to keep only current backup.

I wish to put that into crontab.

Found How to Delete Old Backups from Time Machine on Mac

sudo tmutil delete /Volumes/Time Machine Backups/Backups.backupdb/MacBook\ Pro/2015-07-13-150021/

I don't like to specify Timestamps. I wish to have only one current full backup. This means remove deleted files and versions from backup disk.

3
  • 3
    If you want to do this often I think you are using the wrong tool try Carbon Copy Cloner or Superduper which are designed to just have one copy a
    – mmmmmm
    Apr 22, 2017 at 9:52
  • 2
    But both cost less(and not a massive amount) than the problems of a bad backup or the time you have spent doing this - but for free look at rsync
    – mmmmmm
    Apr 22, 2017 at 15:45
  • Yeah, I Agree: messing with TM usually makes it go wrong. If you only want one copy then use another tool. Also: one copy isn't really a backup. How do you recover a deleted file if your backup is only one copy of what you currently have?
    – benwiggy
    Apr 28, 2021 at 7:00

3 Answers 3

9

As of macOS Big Sur 11.2.3+ - and according to the updated man tmutil - you need to provide the mount point disk and the timestamp of each backup using -d and -t flags, respectively. So the following tweak is needed for the accepted answer:

# assuming you have the backup disk connected & root privileges:
# get the latest backup to exclude from deletion
latest=$(sudo tmutil latestbackup)
# the 4th line after the last space from `tmutil destinationinfo` output
# contains the mount disk name
mountpoint=$(tmutil destinationinfo | sed '4q;d' | sed 's/.* //')
echo "latest is $latest"
# delete all the backups excluding the latest
backups=$(sudo tmutil listbackups)
echo $backups | while read timestamp; do
    if [[ "$timestamp" != "$(basename $latest)" ]]; then
        echo sudo tmutil delete -d $mountpoint -t $timestamp
    fi
done

# if you want to keep the last, say, 3 backups, pipe "sed '$d'" 3-1=2 times:
backups=$(sudo tmutil listbackups | sed '$d' | sed '$d')

It should look like sudo tmutil delete -d /Volumes/TimeMachineBackups -t 2021-04-10-004103.

5
  • MacOS Big Sur 11.2.3 here. For this command: sudo tmutil delete -d 2020-12-16 -t 151046.backup I am getting this error: 151046.backup: Invalid argument (error 22) Apr 26, 2021 at 14:42
  • @bartektartanus what do you get with tmutil listbackups?. For me it's YYYY-MM-DD-hhmmss. In which case you might wanna remove the .backup suffix. I added an edit.
    – aljabadi
    Apr 28, 2021 at 0:57
  • 3
    That won't work. man tmutil says: delete [-d backup_mount_point -t timestamp] [-p path]. The path is only for hfs, but you do need the mount point as well as timestamp. There is no -d argument.
    – Gilby
    Apr 28, 2021 at 5:04
  • @Gilby thanks for pointing this out. I just fixed it.
    – aljabadi
    May 4, 2021 at 0:56
  • 1
    No. When I tried it is had to be sudo tmutil delete -d /Volumes/.timemachine/<UUID>/<date-time>.backup -t <date-time>. See my answer to apple.stackexchange.com/questions/407967/…
    – Gilby
    May 4, 2021 at 2:48
9
#!/bin/bash
latest=$(sudo tmutil latestbackup)
sudo tmutil listbackups | while read backup; do
    if [[ "$backup" != "$latest" ]]; then
        echo sudo tmutil delete "$backup"
    fi
done

Remove the echo once you are sure that the output is correct.

5
  • 1
    Depending on the size of those backups, the grace period granted by sudo may time out while tmutil delete runs. You probably want to just remove sudo from the script and instead run this script with sudo. Also, tmutil listbackups doesn't actually list the "latest" backups, at least not on my system, so your script effectively deletes ALL backups. Nov 3, 2021 at 7:19
  • @DanielSmedegaardBuus Valid point about sudo, I don't understand the second remark though. The goal is to delete all but the latest backup (which gets returned by sudo tmutil latestbackup).
    – nohillside
    Nov 3, 2021 at 8:37
  • My bad, I missed that part in the script XD I did it manually myself. Nov 10, 2021 at 19:25
  • 2
    currently using macOS Ventura 13.1, and sudo tmutil delete {backup} returns error, Specify either a path or a mountpoint and timestamp and Usage: tmutil delete [-d backup_mount_point -t timestamp] [-p path] but adding the option -p before the path works (for some reason I couldn't use the -d mount with -t timestamp options without getting an error, No such file or directory, regardless of combinations attempted)
    – michael
    Jan 8 at 10:54
  • I was too lazy to make a script but but this approach works on macOS Ventura: 1) List all your backups with tmutil listbackups -d /Volumes/BACKUPDRIVE, and then 2) Delete a specific backup with sudo tmutil delete -d /Volumes/BACKUPDRIVE -t 2022-04-05-151520.
    – Bugs Bunny
    Jun 29 at 10:56
1

macOS Sonoma Version 14.0

I have some problems with above bash code. This is my modified code that applies to me.

#!/bin/bash
# Assuming you have the backup disk connected & root privileges:

# Get the latest backup to exclude from deletion
latest=$(sudo tmutil latestbackup)
echo "Latest backup is $latest"

# The 4th line after the last space from `tmutil destinationinfo` output
# contains the mount disk name
mountpoint=$(tmutil destinationinfo | awk '{print $NF}' | sed -n '4p')
echo "The mountponint is $mountpoint"

# Delete all backups excluding the latest
backups=$(sudo tmutil listbackups)
# if you want to keep the last, say, 3 backups, pipe "sed '$d'" 3-1=2 times:
# backups=$(sudo tmutil listbackups | sed '$d' | sed '$d')

echo "$backups" | while read -r backup_path; do
   timestamp=$(basename "$(dirname "$backup_path")" | sed 's/\.backup$//')
   if [ "$timestamp" != "$(basename "$(dirname "$latest")" | sed 's/\.backup$//')" ]; then
      echo "Deleting backup timestamp: $timestamp"
      # if the output is correct, remove the echo
      echo sudo tmutil delete -d "$mountpoint" -t "$timestamp"
   fi
done

If the output is correct, remove the echo in echo sudo tmutil delete -d "$mountpoint" -t "$timestamp"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .