I have my blog backup plugin configured to send backups to a folder on my Google Drive. I am looking for an application that I can set to monitor that folder and delete all but the n most recent subfolders. I have found software that will do that for files, but not for folders.
The following Python script deletes all but the 3 most recently modified directories in another directory. Change the 3 to taste, and change
'mydir' to the directory you want to operate on. Uncomment the last line to actually perform the deletion. It would be reasonable to first run it with the last line still commented out to ensure everything's set up correctly.
import os, shutil dirs = sorted((f for f in os.listdir('mydir') if os.path.isdir(f)), key = os.path.getmtime, reverse = True)[3:] for d in dirs: print("Deleting", d) # shutil.rmtree(d)
You can run this script regularly with a cron or anacron job on Unix-likes, or Task Scheduler on Windows.