{"record":{"id":"ef807b80610360b6","repo":"OrchardCMS/OrchardCore","slug":"cannot-delete-file-path-filesystemstore","errorCode":null,"errorMessage":"Cannot delete file '{path}'.","messagePattern":"Cannot delete file '(.+?)'\\.","errorType":"exception","errorClass":"FileStoreException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs","lineNumber":218,"sourceCode":"\n    public Task<bool> TryDeleteFileAsync(string path)\n    {\n        try\n        {\n            var physicalPath = GetPhysicalPath(path);\n\n            if (!File.Exists(physicalPath))\n            {\n                return Task.FromResult(false);\n            }\n\n            File.Delete(physicalPath);\n\n            return Task.FromResult(true);\n        }\n        catch (Exception ex)\n        {\n            throw new FileStoreException($\"Cannot delete file '{path}'.\", ex);\n        }\n    }\n\n    public Task<bool> TryDeleteDirectoryAsync(string path)\n    {\n        try\n        {\n            var physicalPath = GetPhysicalPath(path);\n\n            if (!Directory.Exists(physicalPath))\n            {\n                return Task.FromResult(false);\n            }\n\n            Directory.Delete(physicalPath, recursive: true);\n\n            return Task.FromResult(true);\n        }","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.FileStorage.FileSystem/FileSystemStore.cs#L200-L236","documentation":"Catch-all in TryDeleteFileAsync: the mapped File.Delete threw an unexpected exception, rethrown as FileStoreException. Missing files are not an error here (the store returns false); this signals the delete itself failed.","triggerScenarios":"TryDeleteFileAsync(path) when the file exists but is locked by another process, the process lacks delete permission, or the path is invalid/read-only.","commonSituations":"File held open by an upload stream or antivirus scanner on Windows; permission denied on the media folder; network share temporarily unavailable.","solutions":["Read the InnerException for the underlying IO error.","Ensure no open FileStream/Stream from GetFileStreamAsync is still holding the file; dispose streams before deleting.","Fix permissions on the storage root or retry after the lock is released."],"exampleFix":"// before\nusing var stream = await store.GetFileStreamAsync(path);\n// file still open\nawait store.TryDeleteFileAsync(path); // fails on Windows\n// after\nusing (var stream = await store.GetFileStreamAsync(path)) { /* read */ }\nawait store.TryDeleteFileAsync(path);","handlingStrategy":"try-catch","validationCode":"var info = await store.GetFileInfoAsync(path);\nif (info is null) return; // nothing to delete, TryDeleteFileAsync would return false anyway","typeGuard":null,"tryCatchPattern":"try { await store.TryDeleteFileAsync(path); }\ncatch (FileStoreException ex)\n{\n    _logger.LogError(ex.InnerException, \"Delete failed for {Path}\", path);\n    throw;\n}","preventionTips":["Dispose all streams opened via GetFileStreamAsync before deleting.","Exclude the storage root from antivirus scanning or use handle-aware retries.","Retry once after a short delay for transient locks."],"tags":["filesystem","io","file-delete"],"backgroundTag":"file-write-failed","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}