Skip to content

Instantly share code, notes, and snippets.

@rosston
Last active February 3, 2023 17:03
Show Gist options
  • Save rosston/e35780d6cf20d216625648f8b627bf48 to your computer and use it in GitHub Desktop.
Save rosston/e35780d6cf20d216625648f8b627bf48 to your computer and use it in GitHub Desktop.
A script to delete Time Machine backups that match a regex
#!/usr/bin/env ruby
# Usage:
# sudo delete_old_tm_backups <path to backup volume> <regex>
#
# Example:
# sudo delete_old_tm_backups /Volumes/backup ^2019
backup_volume = ARGV.shift
delete_regex = Regexp.new(ARGV.shift)
backups = `tmutil listbackups -d #{backup_volume}`
backups_to_delete = backups
.split("\n")
# Extract basename from paths of the following format:
# /Volumes/.timemachine/SOME-UUID/TIMESTAMP.backup/TIMESTAMP.backup
.map { |dir| File.basename(dir, '.backup') }
.filter { |dir| dir =~ delete_regex }
puts "Will delete the following backups from #{backup_volume}:"
puts "\t#{backups_to_delete.join("\n\t")}"
puts
puts "Continue? [y/N]"
confirm = gets
if confirm =~ /^y/
backups_to_delete.each do |backup|
system("tmutil delete -d #{backup_volume} -t #{backup}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment