React-native-fs: unlink doesn't delete the picture

Created on 26 Sep 2017  路  16Comments  路  Source: itinance/react-native-fs

Hello,

I use RNFS.unlink() to delete a picture that was taken by react-native-camera.

Here is my code :

RNFS.exists(filepath)
.then((result) => {
  if(result){
    return RNFS.unlink(filepath)
      .then(() => {
        console.log('la photo a 茅t茅 supprim茅e');
      })
      .catch((err) => {
        console.log("erreur suppression de la photo " + err.message);
      });
  }
})
.catch((err) => {
  console.log(err.message);
});

Where filepath is the name of the picture, for example : _file:///storage/emulated/0/Pictures/IMG_20170926_134921.jpg_ .

After the operation (with no errors), here is the deleted picture :

screenshot_20170926-140213 1

Is there something I didn't understand ?

feedback-wanted more info needed question

Most helpful comment

This issue is still persisting!

I am using the library just for removing a video file fetched from react-native-image-picker, after uploading the file to our server, I need to remove that local file from the user device,

It happens as @chad187 has described, It keeps an existing video in Gallery, it is shown as empty video that could not be displayed.

I have to restart the whole mobile device, to get rid of that empty placeholder!
Any suggestions, even to till Android to refresh the Gallery list?

All 16 comments

I don't know if i got you right. You are deleting an image, right? And the UIImage shows a placeholder then because the image was already deleted? This would be as expected.

Thanks for your answer.
So why does the file is still in the gallery ?

UIImage will cache Images once it was loaded, even if you remove this file later on physical device.
I don't know how smart UIImage is while recognizing if the original file was removed, that it will also automatically purge the cache. You should work with state to tell the UIimage that the URL is not existing and should render then somehing else.

Anyways, a second call to .exists() should return _false_ instead of _true_. Have you tried this out?

Oh, I didn't think of the cache...

That's it, a second call to the functions returns an error: _The file does not exist_ .
After a reboot, the picture does not appear anymore in the gallery.

Thanks for your help and sorry for my stupid question!

I observe the same behavior here.

You should work with state to tell the UIimage that the URL is not existing and should render then somehing else.

@Ignigena, any example for this?

I believe I am having a similar issue but I am trying to delete a video. I get the video taken by react-native-camera and then I do this

 if (filepath != null) {
            RNFS.exists(filepath)
            .then( (result) => {
                console.log("file exists: ", result);

      if(result){
        return RNFS.unlink(filepath)
          .then(() => {
            console.log('FILE DELETED');
            RNFS.exists(filepath)
            .then( (result) => {
              console.log("double checked: ", result);
            });
          })
          // `unlink` will throw an error, if the item to unlink does not exist
          .catch((err) => {
            console.log(err.message);
          });
      }

    })
    .catch((err) => {
      console.log(err.message);
    });
  }

I even threw in that extra exists after the unlink to confirm that the file is gone. It throws no errors and the double check even confirms that it can no longer find the file. However, on my Android phone when I go to photos I can still see blank videos there that will play nothing. Furthermore, when I plug the phone into my pc I can find those files. They are taking up space but they cannot be played. They appear to be corrupt. How can I remove these files completely and not just corrupt the content?

Have you find any work-through to remove this deleted cache image/placeholder?

+1

Has this been addressed. Can we reopen this issue?

This issue is still persisting!

I am using the library just for removing a video file fetched from react-native-image-picker, after uploading the file to our server, I need to remove that local file from the user device,

It happens as @chad187 has described, It keeps an existing video in Gallery, it is shown as empty video that could not be displayed.

I have to restart the whole mobile device, to get rid of that empty placeholder!
Any suggestions, even to till Android to refresh the Gallery list?

If you've still got this issue, use the scanFile API to run the Media Scanner on Android so that it notifies changes to the index, after unlinking the file. #554 has fixed this.

RNFS.unlink(filePath)
  .then(() => {
    console.log('deleted');
    RNFS.scanFile(filePath)
      .then(() => {
        console.log('scanned');
      })
      .catch(err => {
        console.log(err);
      });
  })
  .catch((err) => {         
      console.log(err);
  })

For anyone who may come across this issue in the future who is confused about these gray images left in the device's gallery:

Android maintains a cache of media files on the device. When a file is deleted, the cache is not necessarily updated. See this Stack Overflow post for more info.

This is why you should call RNFS.scanFile(filePath) when you need to update the cache.

I have to try to unlink to remove image but it storing in the pictures folder, please can #anyone advice

What about IOS? In iOS its not deleting after unlink success

For anyone who may come across this issue in the future who is confused about these gray images left in the device's gallery:

Android maintains a cache of media files on the device. When a file is deleted, the cache is not necessarily updated. See this Stack Overflow post for more info.

This is why you should call RNFS.scanFile(filePath) when you need to update the cache.

Gallery and Google Photos remain showing the deleted videos after RNFS.scanFile.

Any other ideas?

What about IOS? In iOS its not deleting after unlink success

is this issue solved ? i am getting same issue
if you solved this issue please help me

Was this page helpful?
0 / 5 - 0 ratings