Clean Windows Update Folder

Please use the script to clean the windows update folder

Please run the script as System User

Steps:

  • Stop Windows Update service and BITS.
  • Rename the SoftwareDistribution Folder
  • Restart the Windows Update and BITS services.
  • Check that a new SoftwareDistribution folder has been created.
  • Delete the renamed folder
def ecmd(CMD):
    import ctypes
    class disable_file_system_redirection:
        _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection
        _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection
        def __enter__(self):
            self.old_value = ctypes.c_long()
            self.success = self._disable(ctypes.byref(self.old_value))
        def __exit__(self, type, value, traceback):
            if self.success:
                self._revert(self.old_value)
    from subprocess import PIPE, Popen
    with disable_file_system_redirection():
        OBJ = Popen(CMD, shell = True, stdout = PIPE, stderr = PIPE)
    out, err = OBJ.communicate()
    ret=OBJ.returncode
    return ret

import os
import shutil
import random
n=random.randint(10000, 20000)
p='C:\\Windows\\SoftwareDistribution'
rn='ren "%s" "%s"'%(p, n)
pn='C:\\Windows\\%s'%n
print 'Stopping Windows Update Service %s %s'%('.'*20, ecmd('net stop wuauserv')==0)
print 'Disabling Windows Update Service %s %s'%('.'*20, ecmd('sc config wuauserv start= disabled')==0)
##print 'Stopping BITS %s %s'%('.'*20, ecmd('net stop BITS')==0)
print 'Disabling BITS %s %s'%('.'*20, ecmd('sc config BITS start= disabled')==0)
print 'Renaming SoftwareDistribution %s %s'%('.'*20, ecmd(rn)==0)
print 'Enabling Windows Update Service %s %s'%('.'*20, ecmd('sc config wuauserv start= auto')==0)
print 'Starting Windows Update Service %s %s'%('.'*20, ecmd('net start wuauserv')==0)
print 'Enabling BITS %s %s'%('.'*20, ecmd('sc config BITS start= auto')==0)
print 'Starting BITS %s %s'%('.'*20, ecmd('net start BITS')==0)
if os.path.isdir(p):
    print '%s %s is available again'%(p, '.'*20)
else:
    print '%s %s is not available again!!'%(p, '.'*20)
if os.path.isdir(pn):
    shutil.rmtree(pn)
    print 'Removing the renamed Folder %s %s True'%(pn, '.'*20)
else:
    print 'Removing the renamed Folder %s %s False'%(pn, '.'*20)

Sample Output:

Script in JSON format:
20170530-Clean-Windows-Update-Folder.json

Screenshot_1.png

20170530-Clean-Windows-Update-Folder.json (2.87 KB)

Does this stop the Windows Update service first of all?

MS Support advise to

  1. Stop Windows Update service and BITS.
  2. Rename the SoftwareDistribution Folder
  3. Restart the Windows Update and BITS services.
  4. Check that a new SoftwareDistribution folder has been created.
  5. Delete the renamed folder.

Hi @Joners

We will update the script as you advised.

Thanks,

Hi @Joners,

Please check the above script https://forum.mspconsortium.com/forum/script-library/5474-clean-windows-update-folder#post5474 and let us know your feedback. We appreciate and welcome your feedback like this.

Thank you.

Hi,

Can you update the script to check if there are any pending Windows updates that need to be installed? If there are then need to stop the script from running as those pending downloaded updates would be deleted by this process.

thanks!

Hi,

Can you update the script to check if there are any pending Windows updates that need to be installed? If there are then need to stop the script from running as those pending downloaded updates would be deleted by this process.

thanks!

Hi @Joners,

Please use this script to clean the windows update folder available in your system.This script will neglect cleaning operation when the windows update is in progress.

https://scripts.comodo.com/frontend/web/topic/clean-windows-update-folder