Deleting files with FileSystem API

  1. SDK Version: 41
  2. Platforms(Android/iOS/web/all): all

I’m trying to delete all downloaded files when the user logs out.

FileSystem.readDirectoryAsync(FileSystem.documentDirectory).then((response) => {
	for (const dir of response) {
		FileSystem.deleteAsync(dir);
	}
});

I run into errors such as “[Unhandled promise rejection: Error: Location ‘361’ isn’t deletable.]”

How do I delete anyway?

Also, there seems to be a directory called RCTAsyncLocalStorage. Is that because I’m using AsyncStore from @react-native-async-store?

Can someone point me in the right direction?

I’m not sure about the root cause, it might be the async storage thing.

A simple workaround would be to create a subdirectory inside documentDirectory and just delete contents of that subdirectory. This makes everything more clear and easy for further modifications (e.g. storing a file which you do not want to delete).

You may also try-catch (or catch() when using then()) the deletion call and skip non-deletable directories.

You may also try-catch (or catch() when using then() ) the deletion call and skip non-deletable directories.

That helped. Thank you!

1 Like