Remove all Time Machine backups except latest one with AppleScript

I want a script that I can run that will delete all previous Time Machine backups, leaving just the latest one.

If I list all contents of [external disk name]/Backups.backupdb/[computer name]/ in name order I can achieve this by deleting all items in the list bar the last 2 which are the latest backup folder followed by the “Latest” alias.

I can get the paths of these folders which I want to delete. If I use the delete AppleScript command I get asked for the root password which I want to avoid inputting. I tried using a shell script to delete them but still no luck.

tell application "Finder"
	set a to items in folder "[external disk name]:Backups.backupdb:[computer name]"	
	if length of a is greater than 2 then repeat with b from 1 to ((length of a) - 2)
		tell me to do shell script "rm -rf " & quoted form of POSIX path of (item b in a as alias) password "[my password]" with administrator privileges
	end repeat	
end tell

This results in a huge long error message in Script Editor listing off all the files and sub folders in the folder I am trying to delete, each one followed by “Operation not permitted”. Sometimes Script Editor even crashes.

I acknowledge that if this script is running while a backup is occurring then something bad will happen… I need to insert a few lines to detect this (by detecting the presence of a .inprogress file).

Any ideas on how to delete these folders?

My understanding of how Time Machine functions makes it very unlikely that you can do this. Time Machine doesn’t back up anything that hasn’t changed, it merely inserts a symbolic link to the file in an earlier backup. Thus each folder contains some part of your backup material. Trashing earlier ones will leave you with the most recent folder full of symlinks to nowhere.

No that’s not right.

All the dated back up folders contain these symbolic links. As long as there is a link somewhere to a particular file it will not be deleted. So deleting the earliest backups will only delete files which are not linked to in a more recent backup, ie old versions of files that have since been changed and backed up again.

I know this because manually deleting the old back up folders using Finder causes no problems. All files are still available on the most recent backup.

From everything I have read (I am not running Leopard) they are “hard links”, not “symbolic links”. They are entirely different beasts. For a technical explanation of hard links and soft links, one might read “Q & A: The difference between hard and soft links” (one of the first few results when I searched for “inode hard link” on Google; it is based on Linux but the mechanisms are largely the same on Mac OS X).

On a technical note, Time Machine is reported to use hardlinks to both files and directories. In many “UNIX-oid” implementations hard links to directories (other than . and …) are not allowed, but they were added to HFS+ and Leopard for Time Machine. Usually hard links to directories are not allowed because they have the potential to introduce loops in the directory hierarchy (beyond the well known loops introduced by . and …) that could choke naïve directory hierarchy traversal routines.

Yes, this is the behavior that you get with hard links.

Actually, “link” is the normal kernel/filesystem term used for “hard links” (as in “a directory is a collection of pairs of names and links to inodes”), but when describing stuff outside the kernel, it is best to say either “hard link” or “symbolic link”/“symlink” to make it clear which being discussed.

I recommend to use another backup solution.
Time Machine’s purpose is to make incremental backups, if you don’t want this,
use SuperDuper! or something similar, which makes smart 1:1 backups

I have considered other solutions but it is the ability to restore a volume from its Time Machine backup on complete loss that keeps me using TM. I mean you can do this while booted off the OSX DVD. Is this possible with other solutions?

SuperDuper! makes 1:1 bootable clones.
You can restore it just by cloning it back.

In case of need you can even boot from the clone

SuperDuper also offers the ability to just backup the files that have changed since the last backup.

I’m curious what your original reasons were for the script you posted. Are you running out of hard drive space? Just trying to keep things clean?