🐍 Python 3.10+
Clean Up Stuck print spooler files (Python)
Reports the size of stuck print spooler files and optionally clears it out. Dry-run by default.
cleanup_spooler_files.py
#!/usr/bin/env python3
"""
cleanup_spooler_files.py
Cleans up stuck print spooler files. Dry-run by default; pass --apply to actually delete.
Usage:
python cleanup_spooler_files.py
python cleanup_spooler_files.py --apply
"""
import argparse
import os
import shutil
from pathlib import Path
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--apply", action="store_true", help="Actually delete (default: dry run)")
args = parser.parse_args()
target = Path(r'C:\Windows\System32\spool\PRINTERS')
if not target.exists():
print(f"{target} does not exist on this machine.")
return
entries = list(target.iterdir())
total_bytes = sum(f.stat().st_size for f in target.rglob("*") if f.is_file())
print(f"{target}: {len(entries)} item(s), {total_bytes / 1024 / 1024:.2f} MB")
if not args.apply:
print("Run again with --apply to actually delete these items.")
return
for entry in entries:
try:
if entry.is_dir():
shutil.rmtree(entry, ignore_errors=True)
else:
entry.unlink(missing_ok=True)
except OSError:
pass
print("Cleanup complete.")
if __name__ == "__main__":
main()