react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

unlink is not working.

Open motogod opened this issue 6 years ago • 13 comments

react-native: 0.58.5 react-native-fs: 2.13.3

test it on simulator IPad Air2 and device IPad

My file path:

file:///var/mobile/Containers/Data/Application/14E28AC2-5E35-4749-B09F-75DBEB0499A4/Library/Caches/Camera/6C6B1C9F-FDAF-4BEB-BEB0-126633336671.jpg

I save the photo to my device's gallery by this:

import { CameraRoll } from 'react-native';

// save it to my device's gallery
CameraRoll.saveToCameraRoll(path);

Then I can see the jpg file on my device's gallery.

When I try to delete it

if (path !== '') {    
  RNFS.unlink(path).then(() => {
    console.log('DELETED PHOTO');
  })
  // `unlink` will throw an error, if the item to unlink does not exist
  .catch((err) => {
    console.log(err.message);
  });
}

I can see DELETED PHOTO on debug mode. But the picture is still on my device's gallery.

I try to change the delete path like this:

const filePath = path.split('///').pop() // removes leading file:///

It is still not working. Any one knows how to fix it ? Thanks.

motogod avatar Aug 27 '19 04:08 motogod

I have the same issue, I was surprised when RNFS gave me a false positive by returning a success message after deleting the image, as opposed to an error meaning the image was not deleted.

jqn avatar Sep 11 '19 04:09 jqn

i also got the same issue

rajtejani avatar Sep 18 '19 05:09 rajtejani

Have you managed to solve this?

perotem7 avatar Feb 03 '20 11:02 perotem7

I found this issue because of write storage permission, so when gave write permission it worked successfully.

rajtejani avatar Feb 05 '20 16:02 rajtejani

@perotem7 Noop I don't use it for a long time.

@rajtejani Thanks for sharing. But I think that all of us had add the permissions all we can and then still get the issue.

motogod avatar Feb 06 '20 02:02 motogod

@motogod Any alternatives?

jackytwh avatar Aug 17 '20 15:08 jackytwh

@jackytwh No

motogod avatar Aug 19 '20 06:08 motogod

Is this still not fixed?

junguksim avatar Apr 13 '23 08:04 junguksim

@junguksim I assume it wasn't an issue ever. Basically, there was some missing statement which was that people hast to take permission from users to get write access and then unlink works. By taking permission I mean is that to prompt user to give storage write permission and not only add in the manifest file.

rajtejani avatar Apr 13 '23 10:04 rajtejani

@rajtejani Thanks for your reply. Can you add more context?

I'm testing it on iOS. And I already added NSPhotoLibraryUsageDescription, which is allowed. Should I add another permission?

junguksim avatar Apr 13 '23 10:04 junguksim

okay by adding NSPhotoLibraryUsageDescription you let the Apple review team know that your app will require storage access. But still, the user hasn't approved this requirement. So you have to use prompt user to give permission for storage access.

rajtejani avatar Apr 13 '23 10:04 rajtejani

@rajtejani Could you provide a code example?

helloukey avatar Oct 26 '24 06:10 helloukey

@helloukey It has been a long time since I faced this issue. But if you are still facing it, then let me explain what solution I tried to implement.

Basically, to delete a file, you must have storage write permission given by the user through the permission prompt.

So the code would be like this

check(PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE).then((status) => {
  switch (status) {
    case RESULTS.UNAVAILABLE:
      return console.log('This feature is not available (on this device / in this context)');
    case RESULTS.DENIED:
      return console.log('The permission has not been requested / is denied but requestable');
    case RESULTS.BLOCKED:
      return console.log('The permission is denied and not requestable');
    case RESULTS.GRANTED:
       RNFS.unlink(path)
         .then(() => {
           console.log('FILE DELETED');
        })
    // `unlink` will throw an error if the item to unlink does not exist
    .catch((err) => {
        console.log(err.message);
     });
    case RESULTS.LIMITED:
      return console.log('The permission is granted but with limitations');
  }
});

rajtejani avatar Oct 27 '24 07:10 rajtejani