{"record":{"id":"a44b99f3eb4b7fa4","repo":"nostra13/Android-Universal-Image-Loader","slug":"failed-to-delete-file","errorCode":null,"errorMessage":"failed to delete {file}","messagePattern":"failed to delete (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/DiskLruCache.java","lineNumber":617,"sourceCode":"\n\t/**\n\t * Drops the entry for {@code key} if it exists and can be removed. Entries\n\t * actively being edited cannot be removed.\n\t *\n\t * @return true if an entry was removed.\n\t */\n\tpublic synchronized boolean remove(String key) throws IOException {\n\t\tcheckNotClosed();\n\t\tvalidateKey(key);\n\t\tEntry entry = lruEntries.get(key);\n\t\tif (entry == null || entry.currentEditor != null) {\n\t\t\treturn false;\n\t\t}\n\n\t\tfor (int i = 0; i < valueCount; i++) {\n\t\t\tFile file = entry.getCleanFile(i);\n\t\t\tif (file.exists() && !file.delete()) {\n\t\t\t\tthrow new IOException(\"failed to delete \" + file);\n\t\t\t}\n\t\t\tsize -= entry.lengths[i];\n\t\t\tfileCount--;\n\t\t\tentry.lengths[i] = 0;\n\t\t}\n\n\t\tredundantOpCount++;\n\t\tjournalWriter.append(REMOVE + ' ' + key + '\\n');\n\t\tlruEntries.remove(key);\n\n\t\tif (journalRebuildRequired()) {\n\t\t\texecutorService.submit(cleanupCallable);\n\t\t}\n\n\t\treturn true;\n\t}\n\n\t/** Returns true if this cache has been closed. */","sourceCodeStart":599,"sourceCodeEnd":635,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/DiskLruCache.java#L599-L635","documentation":"IOException thrown by DiskLruCache.remove() when a clean file of the entry exists but File.delete() returns false. The cache keeps its size accounting consistent with the filesystem, so a file it cannot delete is treated as a hard failure rather than silently leaking the entry.","triggerScenarios":"cache.remove(key) while another process/stream still holds the file open (Windows or a still-open Snapshot InputStream), read-only filesystem or SELinux denial on the cache dir, or the file being deleted concurrently by an external cleanup.","commonSituations":"Android device with full/damaged emulated storage; a Snapshot's InputStream not closed before remove(); cache directory on external storage unmounted mid-operation; running on a desktop JVM against a locked file.","solutions":["Close any open Snapshot streams (snapshot.close()) for the entry before calling remove().","Verify the cache directory is writable and on healthy storage; prefer context.getCacheDir() (internal) over external storage.","As a last resort, catch the IOException and wipe/rebuild the whole cache directory, since accounting is now untrustworthy."],"exampleFix":"// before\nSnapshot snap = cache.get(key);\nInputStream is = snap.getInputStream(0);\n// ... use is, never close ...\ncache.remove(key); // may fail: file still open\n\n// after\nSnapshot snap = cache.get(key);\ntry (InputStream is = snap.getInputStream(0)) {\n    // ... use is ...\n} finally {\n    snap.close();\n}\ncache.remove(key);","handlingStrategy":"try-catch","validationCode":"// Ensure no open streams on the entry before removal\nSnapshot snap = cache.get(key);\nif (snap != null) snap.close(); // releases open InputStreams\ncache.remove(key);","typeGuard":null,"tryCatchPattern":"try {\n    cache.remove(key);\n} catch (IOException deleteFailed) {\n    // size accounting is now suspect: rebuild the cache wholesale\n    try { cache.delete(); } catch (IOException wipeFailed) { log(wipeFailed); }\n    cache = reopenFreshCache(dir);\n}","preventionTips":["Always close Snapshot InputStreams (try-with-resources) before removing entries.","Keep the cache on internal storage (context.getCacheDir()) where deletion is reliable.","Monitor free space — a nearly-full or damaged volume is the top cause of failed deletes."],"tags":["disk-cache","lru","io","filesystem","resource-leak"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}