{"record":{"id":"733ce742fe544344","repo":"pentaho/pentaho-kettle","slug":"we-can-not-delete-file","errorCode":null,"errorMessage":"We can not delete file [","messagePattern":"We can not delete file \\[","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1962,"sourceCode":"    }\n  }\n\n  public static void deleteFile( Bowl bowl, ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,\n    Object FunctionContext ) {\n\n    try {\n      if ( ArgList.length == 1 && !isNull( ArgList[0] ) && !isUndefined( ArgList[0] ) ) {\n        // Object act = actualObject.get(\"_step_\", actualObject);\n        // ScriptValuesMod act = (ScriptValuesMod)Context.toType(scm_delete, ScriptValuesMod.class);\n\n        FileObject fileObject = null;\n\n        try {\n          fileObject = KettleVFS.getInstance( bowl ).getFileObject( (String) ArgList[0] );\n          if ( fileObject.exists() ) {\n            if ( fileObject.getType() == FileType.FILE ) {\n              if ( !fileObject.delete() ) {\n                throw new RuntimeException( \"We can not delete file [\" + (String) ArgList[0] + \"]!\" );\n              }\n            }\n\n          } else {\n            throw new RuntimeException( \"file [\" + ArgList[0] + \"] can not be found!\" );\n          }\n        } catch ( IOException e ) {\n          throw new RuntimeException( \"The function call deleteFile is not valid.\" );\n        } finally {\n          if ( fileObject != null ) {\n            try {\n              fileObject.close();\n            } catch ( Exception e ) {\n              // Ignore errors\n            }\n          }\n        }\n","sourceCodeStart":1944,"sourceCodeEnd":1980,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1944-L1980","documentation":"The deleteFile() script helper resolves the file via KettleVFS, checks it exists and is a regular file, then calls fileObject.delete(). If the VFS layer reports the delete() call as unsuccessful (returned false), the helper throws 'We can not delete file [name]!'.","triggerScenarios":"Calling deleteFile('path') where the target exists as a FILE but fileObject.delete() returns false — typically because the OS refuses the delete (open handles, read-only attribute, or lack of write permission on the parent directory).","commonSituations":"Deleting a file that another process (or an earlier step in the same transformation) still holds open; deleting files on read-only mounts or without permissions; Windows file locking; deleting files currently being written by a previous transform step.","solutions":["Ensure no other step/process holds the file open; close streams or re-order steps so the file is finished being read/written before deletion.","Verify the process user has write permission on the file's parent directory (delete requires directory write access).","Check the file is not read-only and remove the read-only flag if needed.","Wrap deletion with retry logic or check existence again after failure; if it must succeed, delete manually and treat the script call as best-effort.","Consider using a dedicated 'Delete files' transformation step which reports errors through Kettle's normal error handling."],"exampleFix":"// before\ndeleteFile('/tmp/lock.txt'); // file still open by earlier step\n// after\n// ensure previous step finished and stream is closed, then:\nvar f = new java.io.File('/tmp/lock.txt');\nif (f.exists()) { java.nio.file.Files.delete(java.nio.file.Paths.get('/tmp/lock.txt')); }","handlingStrategy":"try-catch","validationCode":"// ensure the target is a deletable file first\nif (!fileExists(path)) { /* skip */ }","typeGuard":"function isDeletableStringPath(p) { return typeof p === 'string' && p.length > 0; }","tryCatchPattern":"try { deleteFile(path); } catch (e) { // retry after delay or log and continue\n}","preventionTips":["Close all streams reading the file before deleting","Check parent-directory write permissions","Avoid deleting files still owned by earlier running steps"],"tags":["file-io","delete","vfs","permissions"],"backgroundTag":"file-delete-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}