{"record":{"id":"07a1cb641f4cd855","repo":"nostra13/Android-Universal-Image-Loader","slug":"failed-to-delete-file-file","errorCode":null,"errorMessage":"failed to delete file: {file}","messagePattern":"failed to delete file: (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/Util.java","lineNumber":61,"sourceCode":"\t\t\treader.close();\n\t\t}\n\t}\n\n\t/**\n\t * Deletes the contents of {@code dir}. Throws an IOException if any file\n\t * could not be deleted, or if {@code dir} is not a readable directory.\n\t */\n\tstatic void deleteContents(File dir) throws IOException {\n\t\tFile[] files = dir.listFiles();\n\t\tif (files == null) {\n\t\t\tthrow new IOException(\"not a readable directory: \" + dir);\n\t\t}\n\t\tfor (File file : files) {\n\t\t\tif (file.isDirectory()) {\n\t\t\t\tdeleteContents(file);\n\t\t\t}\n\t\t\tif (!file.delete()) {\n\t\t\t\tthrow new IOException(\"failed to delete file: \" + file);\n\t\t\t}\n\t\t}\n\t}\n\n\tstatic void closeQuietly(/*Auto*/Closeable closeable) {\n\t\tif (closeable != null) {\n\t\t\ttry {\n\t\t\t\tcloseable.close();\n\t\t\t} catch (RuntimeException rethrown) {\n\t\t\t\tthrow rethrown;\n\t\t\t} catch (Exception ignored) {\n\t\t\t}\n\t\t}\n\t}\n}","sourceCodeStart":43,"sourceCodeEnd":76,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/Util.java#L43-L76","documentation":"Thrown by Util.deleteContents when File.delete() returns false for an entry inside the cache directory while the LruDiskCache (DiskLruCache) is clearing its directory during initialization or maintenance. It means the OS refused the delete: the file is locked by another process/handle, the directory entry is read-only, or the filesystem is in a bad state. Because deleteContents recurses depth-first, the offending nested File is included in the message.","triggerScenarios":"DiskLruCache initialization calling deleteContents(directory) on a cache dir containing undeletable entries: a file currently open by another thread/process, read-only media, or a file created by a different app uid in a shared cache path.","commonSituations":"Two app processes sharing one external-storage cache directory; a crash left a partially-written journal and a stale file handle; SD card mounted read-only or full; OEM storage quirks; antivirus/adb pulling files out of the cache dir while the app starts.","solutions":["Use a private, per-process cache directory (context.getCacheDir() based) so nothing else holds handles to files in it","Ensure only one ImageLoader instance/process owns the disk cache directory","Free storage / remount the volume writable, then restart the app","Catch IOException around ImageLoader init and continue with memory-cache-only configuration"],"exampleFix":"// before\nImageLoader.getInstance().init(config); // crashes stack with failed to delete file: ...\n\n// after\ntry {\n    ImageLoader.getInstance().init(config);\n} catch (IOException e) {\n    L.e(\"disk cache unusable, falling back\", e);\n    ImageLoader.getInstance().init(new ImageLoaderConfiguration.Builder(context)\n            .memoryCacheSizePercentage(25)\n            .build()); // no disk cache\n}","handlingStrategy":"fallback","validationCode":"boolean canWrite = cacheDir.canWrite();\n// best-effort probe: try deleting a temp file\nFile probe = new File(cacheDir, \".probe\");\ntry {\n    if (probe.createNewFile() && probe.delete()) canWrite = true; else canWrite = false;\n} catch (IOException e) { canWrite = false; }","typeGuard":null,"tryCatchPattern":"try {\n    ImageLoader.getInstance().init(config);\n} catch (IOException e) {\n    L.e(\"disk cache init failed: \" + e.getMessage(), e);\n    ImageLoader.getInstance().init(memoryOnlyConfig);\n}","preventionTips":["Keep the disk cache in app-private storage owned solely by your process","Initialize ImageLoader in Application.onCreate and catch IOException there to degrade gracefully","Monitor free storage; a full read-only volume is a common root cause"],"tags":["disk-cache","io","filesystem","multi-process","storage"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}