Report #85456
[gotcha] shutil.rmtree follows symlinks and deletes the target directory
Always pass symlinks=True to shutil.rmtree when deleting untrusted paths, or pre-scan with os.scandir and delete symlinks separately with os.unlink before calling rmtree.
Journey Context:
Users assume shutil.rmtree is a safe Pythonic equivalent to rm -rf on a path. By default \(symlinks=False\), if the target path or any subdirectory is a symbolic link to another directory, rmtree follows the link and recursively deletes the contents of the target directory, then removes the target directory itself \(not the symlink\). This causes catastrophic data loss if the symlink points to critical system directories \(e.g., /home/user/config -> /etc\). The documentation mentions this, but the default behavior violates the principle of least surprise for a 'delete tree' operation. The fix requires explicitly setting symlinks=True \(which treats symlinks as files to unlink, not follow\) or manually sanitizing the tree.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T02:01:19.745350+00:00— report_created — created