{"record":{"id":"c7ffda6ec3dc7a4f","repo":"dotnet/wpf","slug":"sr-cannotdelete","errorCode":null,"errorMessage":"SR.CanNotDelete","messagePattern":"SR\\.CanNotDelete","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs","lineNumber":792,"sourceCode":"            }\n        }\n        \n        // Make the call to the underlying OLE mechanism to remove the element.            \n        try\n        {\n            core.safeIStorage.DestroyElement( elementNameInternal );\n        }\n        catch( COMException e )\n        {\n            if( e.ErrorCode == SafeNativeCompoundFileConstants.STG_E_ACCESSDENIED )\n            {\n                throw new UnauthorizedAccessException(\n                    SR.CanNotDeleteAccessDenied,\n                    e );\n            }\n            else\n            {\n                throw new IOException(\n                    SR.CanNotDelete,\n                    e );\n            }\n        }\n\n        // Invalidate enumerators\n        InvalidateEnumerators();\n\n            // Remove the now-meaningless name, which also signifies disposed status.\n            if (deadElementWalking is StorageInfoCore deadStorageInfoCore)\n            {\n\n                // Erase this storage's existence\n                deadStorageInfoCore.storageName = null;\n                if (null != deadStorageInfoCore.safeIStorage)\n                {\n                    ((IDisposable)deadStorageInfoCore.safeIStorage).Dispose();\n                    deadStorageInfoCore.safeIStorage = null;","sourceCodeStart":774,"sourceCodeEnd":810,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs#L774-L810","documentation":"StorageInfo.DeleteSubStorage catches a COMException from IStorage::DestroyElement whose error code is not STG_E_ACCESSDENIED and throws IOException with SR.CanNotDelete. It is the generic 'we could not delete this sub-storage' path for any COM failure other than access denied (e.g. element busy, name invalid, file corruption).","triggerScenarios":"Calling DeleteSubStorage/Delete on a storage whose IStorage::DestroyElement fails with any HRESULT other than STG_E_ACCESSDENIED — corrupt compound file, storage opened in a sharing-incompatible mode, or a name that does not resolve.","commonSituations":"Deleting entries in a truncated or partially-written compound document; deleting a sub-storage while enumerators/streams are still bound to it in conflicting modes; media or file-system I/O errors.","solutions":["Inspect the inner COMException's HResult to identify the underlying STG_E_* code.","Close all StreamInfo/StorageInfo objects and enumerators derived from the container before deleting.","Verify the file is not corrupt by opening it in the owning application or repairing it.","Retry on a writable copy of the file if the original medium is suspect."],"exampleFix":"// before\nstorageInfo.DeleteSubStorage(name); // throws IOException\n// after\ntry { storageInfo.DeleteSubStorage(name); }\ncatch (IOException e) when (e.InnerException is COMException ce) {\n    // inspect ce.HResult (STG_E_* code) and handle/retry\n}","handlingStrategy":"try-catch","validationCode":"if (!File.Exists(path)) throw new FileNotFoundException(path);\nif (new FileInfo(path).Length == 0) throw new InvalidDataException(\"Empty compound file\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Close all child StreamInfo/StorageInfo/enumerators before deleting.","Validate the file opens in the owning application (not corrupt).","Work on a writable local copy rather than the original medium."],"tags":["io","compound-file","com","file-delete"],"backgroundTag":"file-write-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}