Skip to content

Instantly share code, notes, and snippets.

@grahamgilbert
Created May 9, 2018 16:35
Show Gist options
  • Save grahamgilbert/9ae1ea78add169aca6cfecdfd168cd36 to your computer and use it in GitHub Desktop.
Save grahamgilbert/9ae1ea78add169aca6cfecdfd168cd36 to your computer and use it in GitHub Desktop.
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