{"record":{"id":"f9693ae3d7e31f85","repo":"dbt-labs/dbt-core","slug":"failed-to-remove-stale-directory","errorCode":null,"errorMessage":"Failed to remove stale directory {}: {}","messagePattern":"Failed to remove stale directory (.+?): (.+?)","errorType":"exception","errorClass":"minijinja::Error (InvalidOperation)","httpStatus":null,"severity":"error","filePath":"crates/dbt-jinja-utils/src/phases/run/run_node_context.rs","lineNumber":769,"sourceCode":"                        format!(\"Failed to remove stale file {}: {}\", ancestor.display(), e),\n                    ));\n                }\n                break;\n            }\n        }\n        if let Err(e) = fs::create_dir_all(parent) {\n            return Err(Error::new(\n                ErrorKind::InvalidOperation,\n                format!(\"Failed to create directory {}: {}\", parent.display(), e),\n            ));\n        }\n    }\n\n    // A stale directory sitting where we now write a flat file fails with EISDIR.\n    if full_path.is_dir()\n        && let Err(e) = fs::remove_dir_all(full_path)\n    {\n        return Err(Error::new(\n            ErrorKind::InvalidOperation,\n            format!(\n                \"Failed to remove stale directory {}: {}\",\n                full_path.display(),\n                e\n            ),\n        ));\n    }\n\n    match fs::write(full_path, payload) {\n        Ok(_) => Ok(()),\n        Err(e) => Err(Error::new(\n            ErrorKind::InvalidOperation,\n            format!(\"Failed to write to {}: {}\", full_path.display(), e),\n        )),\n    }\n}\n","sourceCodeStart":751,"sourceCodeEnd":787,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-jinja-utils/src/phases/run/run_node_context.rs#L751-L787","documentation":"When the destination path itself is a stale directory (from a previous nested artifact layout) and the current resource writes a flat file, write_file removes the directory with remove_dir_all; failure raises this error with the path and OS error (e.g. permission denied or non-empty removal issues).","triggerScenarios":"full_path (e.g. target/run/some_model) is an existing directory from an earlier layout, and fs::remove_dir_all fails due to permissions, files held open by other processes, or a read-only filesystem.","commonSituations":"Upgrading/downgrading dbt versions where artifact layout changed; parallel processes racing on the same target/ directory; directories owned by a different user in shared CI workspaces.","solutions":["Remove the stale directory manually using the path from the message: rm -rf <path>.","Fix ownership/permissions of the target/ tree for the running user.","Avoid concurrent runs sharing one target/ directory; use distinct --target-path or profiles per job.","Close processes (watchers, editors, antivirus) holding handles inside the directory and rerun."],"exampleFix":"// before\n# dbt run -> Failed to remove stale directory target/run/my_model: Permission denied\n// after\nsudo chown -R $(whoami) target/ && rm -rf target/run/my_model && dbt run","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if full_path.is_dir() {\n    if let Err(e) = fs::remove_dir_all(full_path) {\n        if e.kind() == std::io::ErrorKind::PermissionDenied {\n            // hint: chown target/ and remove the stale directory manually\n        }\n        return Err(wrap(full_path, e));\n    }\n}","preventionTips":["Delete target/ when switching between dbt versions with different artifact layouts.","Never share one target/ directory across concurrent dbt processes.","Keep target/ ownership consistent with the user running dbt."],"tags":["filesystem","stale-artifacts","permissions","dbt"],"backgroundTag":"file-write-failed","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}