The old file is being deleted from a different directory than where the test is running. To fix it:
old_file_path = Path(sample.test_suite.path)
if old_file_path.exists():
old_file_path.unlink()
new_file_path = Path(sample.test_suite.path)
This should fix the issue because:
It uses the actual paths from Django's model instead of constructing them It properly cleans up the old file using the correct path.