title: deleting a file on Maui app platform Android
body: <p>I have a maui app which reads a data log on an SD card (in Android the SD card is in a USB reader attached - filemanagers and the maui file picker happily see hte usb external drive and read and open file)
  Info is extracted from the file (csv)  written to SQL then the file is backed up to cloud
  It is important now to remove datalog from the SD card -</p>
  <p>the problem appears to be only on Android and even then it performs the deletion on teh emulator as expected. On Android &gt;10   (Iam targetting 11 sdk 31) on real phones it says it has deleted the file (file.exists returns false)   but the file still exists on sd card</p>
  <p>I know Android tightened up permissions and I have the request permissions in the manifest file I also have code whihc requests user to confirm permission but still delete does not work as stated in Microsoft Docs</p>
  <p>I am aware but dont quite understand the mechanism where Android makes a copy of an opened file but this doesnt seem relevant as Ive already opened and read the file.
  I am also not sure this has anything to do with the fact that I am trying to delete a file on external USB-SD storage - the problem persists if I move the datalog to internal storage such as download.</p>
  <p>Android permissions:   (you can see Ive thrown kitchen sink at it ;-)</p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.MANAGE_EXTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.READ_INTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.WRITE_INTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.MANAGE_USB&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.MANAGE_EXTERNAL_STORAGE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.USE_CREDENTIALS&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.MOUNT_UNMOUNT_FILESYSTEMS&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.MOUNT_FORMAT_FILESYSTEMS&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.MANAGE_DOCUMENTS&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
  </code></pre>
  <p>Key code is shown below    (in this case if the datalog contains less than 300 lines it does not contain useful data so is to be deleted  - but the principle is the same once a datalog is fully processed and to be disposed of
  NB this is on MAUI using VS 2022 17.3 preview 6
  fwiw the xamarin Android sdk is v 13
  the file delete works on windows - I haven't tried IOS yet as I have had huge difficulty getting my MAC to sync with my VS on windows</p>
  <p>In summary the c# code is doing what it says in microsoft docs deleting and reporting file does not exist but in actual fact not doing that!!   This is beyond me</p>
  <p>does anyone have any ideas please</p>
  <pre><code>       var ff1 = System.IO.File.ReadAllLines(csvorig);
         var csvlen= ff1.Length;
          if (csvlen &lt; 300)   // one line per second means 5 mins ignore
          {
              var nl = System.Environment.NewLine;
              var answ = await DisplayAlert(dbpath + nl + &quot;Small file&quot;, &quot;the file you've 
                 selected contains no flight&quot; + nl + &quot;prob cause = switching on avionics for &lt; 
                 5min &quot; + nl + &quot; file should be deleted&quot;, &quot;DELETE&quot;, &quot;ignore&quot;);
          if (answ)
          {
          status = await Permissions.CheckStatusAsync&lt;Permissions.StorageWrite&gt;();
          if (status == PermissionStatus.Denied)
          {
              PermissionStatus status2=awaitPermissions.RequestAsync&lt;Permissions.StorageWrite&gt;();
          }
                /// check permissions AGAIN !! in case problem there
          status = await Permissions.CheckStatusAsync&lt;Permissions.StorageWrite&gt;();
          if (status == PermissionStatus.Denied)
          {
          await DisplayAlert(&quot;warn&quot;, &quot;Sorry cannot grant permission \n you will need to delete this file using filemanager \n&quot; + csvorig, &quot;cancel&quot;);
          return;
          }
          try
              {
               // ensure that file attribs are normal // saw this in one post on file 
               //deleting but not ref maui
              File.SetAttributes(csvorig, FileAttributes.Normal);
              File.Delete(csvorig);
              }
          catch (Exception ee)
              {
            // code never throws exception
              var m = ee.Message;
              await DisplayAlert(&quot;&quot;, m, &quot;Cancel&quot;);
              }
      
          if (File.Exists(csvorig))
          {           
              await DisplayAlert(&quot;warn&quot;, &quot;failed to delete&quot;, &quot;cancel&quot;);
          }
          else
          { // file is ALWAYS reported as 'not existing' but is still there next 
            //time use filepicker
          await DisplayAlert(csvorig, &quot;has been deleted&quot;, &quot;back&quot;); }
          }
      }
  
  
  
  
  
  
  
  
  
  
  </code></pre>