{"record":{"id":"e6bd1d37e2484f9f","repo":"dotnet/wpf","slug":"sr-cannotdeletenonemptystorage","errorCode":null,"errorMessage":"SR.CanNotDeleteNonEmptyStorage","messagePattern":"SR\\.CanNotDeleteNonEmptyStorage","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs","lineNumber":701,"sourceCode":"    /// </summary>\n    /// <param name=\"recursive\">Whether to recursive delete all existing content</param>\n    /// <param name=\"name\">Name of storage</param>\n    internal bool Delete( bool recursive , string name)\n    {\n        bool storageDeleted = false;\n        CheckDisposedStatus();\n        if( null == parentStorage )\n        {\n            // We are the root storage, you can't \"delete\" the root storage!\n            throw new InvalidOperationException(\n                SR.CanNotDeleteRoot);\n        }\n\n        if( InternalExists(name) )\n        {\n            if( !recursive && !StorageIsEmpty())\n            {\n                throw new IOException(\n                    SR.CanNotDeleteNonEmptyStorage);\n            }\n\n            InvalidateEnumerators();\n            // Go ahead and delete \"this\" storage\n            parentStorage.DestroyElement( name );\n            storageDeleted = true;\n        }\n        //We will not throw exceptions if the storage does not exist. This is to be consistent with Package.DeletePart.\n        \n        return storageDeleted;\n    }\n\n    /// <summary>\n    /// When a substorage is getting deleted, its references in the dataspacemanager's transform definition are removed.\n    /// This is called recursively because the DeleteSubStorage deletes all its children by default.\n    /// </summary>\n    internal void RemoveSubStorageEntryFromDataSpaceMap(StorageInfo storageInfo)","sourceCodeStart":683,"sourceCodeEnd":719,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/IO/Packaging/CompoundFile/StorageInfo.cs#L683-L719","documentation":"Thrown by StorageInfo.Delete when the storage still contains child storages or streams and recursive=false. The compound file refuses to destroy a non-empty element unless the caller explicitly asks for recursive deletion, preventing accidental data loss inside the package.","triggerScenarios":"Calling Delete() (non-recursive) on a storage that has children; deleting a '\\u0006DataSpaces' or TransformInfo storage that still contains streams.","commonSituations":"Cleanup code assuming storages are empty; incremental package writers removing intermediate storages that were reused and repopulated; deleting a page/fragment storage with content still inside.","solutions":["Pass recursive: true to Delete when intentional full removal is wanted","Check StorageIsEmpty() first and decide what to do with children","Enumerate and delete children (streams and sub-storages) explicitly before calling Delete","Ensure prior write steps actually consumed/emptied the storage before cleanup"],"exampleFix":"// before\nstorage.Delete();\n// after\nstorage.Delete(recursive: true); // or check storage.StorageIsEmpty() first","handlingStrategy":"validation","validationCode":"if (!storage.StorageIsEmpty() && !recursive)\n    throw new InvalidOperationException(\"Refusing to delete non-empty storage without recursive\");","typeGuard":null,"tryCatchPattern":"try { storage.Delete(recursive); }\ncatch (IOException ex) { /* storage non-empty: enumerate children or request recursive */ }","preventionTips":["Decide explicitly whether deletion should be recursive","Check StorageIsEmpty() before non-recursive Delete","Verify cleanup expectations: intermediate storages may have been repopulated"],"tags":["compound-file","storage","delete","non-empty"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}