Hello Friends,

Welcome To Infinitbility!

This article will help you to delete your file in react native using react-native-fs library.

Here, we are use react-native-fs library to check file available or not and delete file.

Let’s start today’s topic How to delete file in react native

react-native-fs provide unlink component to delete file available or not it will throw error and it works on both platform (android, and iOS).

unlink(filepath: string): Promise<void>

react-native-fs unlink function required filepath to delete files and return with promise whitch means you have to use await for get proper result.

Installation

First we have need to install react-native-fs library on our project.

npm install react-native-fs --save

Extra steps for iOS Installation

  • add below code to your podfile
pod 'RNFS', :path => '../node_modules/react-native-fs'
  • install library
pod install

Here, i am sharing how i delete file using react-native-fs unlink in react native, you have to just pass filepath to delete function with await.

One more thing, first you have to check file exists and then call unlink else you will get error.

let’s understand with code example.

  • add below code top of your file
// require the module
var RNFS = require('react-native-fs');
  • exists usebility
async function delteFiles(filepath) {
    let exists = await RNFS.exists(filepath);
    if(exists){
        // exists call delete
        await RNFS.unlink(filepath);
        console.log("File Deleted");
    }else{
        console.log("File Not Available")
    }
}

Thanks for reading…