fun deleteFile(context: Context, file: File): Boolean { var success: Boolean = file.delete() if (!success && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { val document: DocumentFile = getDocumentFile(context, file, false, false) success = document != null && document.delete() } if (!success && Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { val resolver: ContentResolver = context.getContentResolver() success = try { val uri: Uri? = null if (uri != null) { resolver.delete(uri, null, null) } !file.exists() } catch (e: Exception) { Log.e(TAG, "Error when deleting file " + file.getAbsolutePath(), e) return false } } if (success) scanFile(context, arrayOf(file.getPath())) return success }
Delete a file . May be even on external SD card .