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

Remove all files from tmp directory

Open mtwallet opened this issue 10 years ago • 8 comments

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.

mtwallet avatar Jun 26 '16 08:06 mtwallet

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

johanneslumpe avatar Jun 27 '16 05:06 johanneslumpe

@johanneslumpe How would you do that?

cvarley100 avatar Oct 23 '17 15:10 cvarley100

@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>)

erhathaway avatar Nov 14 '17 22:11 erhathaway

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

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

Not working

ydykevin avatar Dec 05 '18 04:12 ydykevin

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

Udbhav12 avatar Jun 20 '19 11:06 Udbhav12

@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 avatar Jun 20 '19 16:06 ashar-7

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

thanks

matamicen avatar Oct 31 '20 00:10 matamicen

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

Nope I haven't used react since a year

ashar-7 avatar Nov 06 '20 04:11 ashar-7