Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clean out old apple updates (older than 24 hours) because softwareupdate often refuses to install them
#!/usr/bin/python
"""
Removes cached apple updates that are older than 24 hours
"""
import datetime
import os
import shutil
import sys
def main():
"""
Main event
"""
update_dir = '/Library/Updates'
# If the directory isn't there, might as well exit
if not os.path.exists(update_dir):
sys.exit()
now = datetime.datetime.now()
day_ago = now - datetime.timedelta(hours=24)
# Get all directories in /L/Updates
for item in os.listdir(update_dir):
if item == 'PreflightContainers':
continue
if os.path.isdir(os.path.join(update_dir, item)):
modification_time = datetime.datetime.fromtimestamp(
os.stat(os.path.join(update_dir, item)).st_mtime)
if modification_time < day_ago:
# This is ick, but we would rather eat the error than
# halt the Munki run
try:
shutil.rmtree(os.path.join(update_dir, item))
except Exception:
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.