public static boolean deleteFile(Context context,@NonNull final File file){
  boolean success=file.delete();
  if (!success && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    DocumentFile document=getDocumentFile(context,file,false,false);
    success=document != null && document.delete();
  }
  if (!success && Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
    ContentResolver resolver=context.getContentResolver();
    try {
      Uri uri=null;
      if (uri != null) {
        resolver.delete(uri,null,null);
      }
      success=!file.exists();
    }
 catch (    Exception e) {
      Log.e(TAG,"Error when deleting file " + file.getAbsolutePath(),e);
      return false;
    }
  }
  if (success)   scanFile(context,new String[]{file.getPath()});
  return success;
}

public static boolean deleteFile ( Context context , @ NonNull final File file ) { boolean success = file . delete ( ) ; if ( ! success && Build . VERSION . SDK_INT >= Build . VERSION_CODES . LOLLIPOP ) { DocumentFile document = getDocumentFile ( context , file , false , false ) ; success = document != null && document . delete ( ) ; } if ( ! success && Build . VERSION . SDK_INT == Build . VERSION_CODES . KITKAT ) { ContentResolver resolver = context . getContentResolver ( ) ; try { Uri uri = null ; if ( uri != null ) { resolver . delete ( uri , null , null ) ; } success = ! file . exists ( ) ; } catch ( Exception e ) { Log . e ( TAG , "Error when deleting file " + file . getAbsolutePath ( ) , e ) ; return false ; } } if ( success ) scanFile ( context , new String [ ] { file . getPath ( ) } ) ; return success ; }
delete a file . may be even on external sd card .
