React-native-fs: Remove all files from tmp directory

Created on 26 Jun 2016  路  8Comments  路  Source: itinance/react-native-fs

Hi,

Is there a way to delete all files within a temp directory? I know I can access the directory with RNFS.TemporaryDirectoryPath but instead of adding a ref to a single file I'd like to remove all files contained within.

Most helpful comment

@cvarley100 If you haven't figured this out yet, you could just read the files at a directory readdir(<dir path>) and then do an unlink on each file in the result paths.forEach(p => unlink(p)).

However, you shouldn't need to do this. The documentation says unlink can recursively delete directories. Just feed it the directory you want to remove: unlink(<dir path>)

All 8 comments

The way to do that right now is, like you already mentioned, looping over the contents and removing each file on its own.

@johanneslumpe How would you do that?

@cvarley100 If you haven't figured this out yet, you could just read the files at a directory readdir(<dir path>) and then do an unlink on each file in the result paths.forEach(p => unlink(p)).

However, you shouldn't need to do this. The documentation says unlink can recursively delete directories. Just feed it the directory you want to remove: unlink(<dir path>)

How do I delete files with same extension?
I've tried

RNFS.unlink(RNFS.DocumentDirectoryPath+'/*.jpg').then(()=>{
console.log('finish delete');
});

Not working

Is there any better way to do this now ? Or do we still need to iterate over and delete each file ?

@Udbhav12 This works for me: RNFS.unlink(/path/to/dir/). This deletes the whole directory. After this, you can create the directory again using RNFS.mkdir

@ashar-7 Do you know if RNFS.unlink is working on Android 10 ?

thanks

@ashar-7 Do you know if RNFS.unlink is working on Android 10 ?

Nope I haven't used react since a year

Was this page helpful?
0 / 5 - 0 ratings