FileSystem.deleteAsync(fileUri) is not working in componentWillUnmount

  1. SDK Version: 44.0.5
  2. Platforms: Android/iOS

I am storing a file in FileSystem.cacheDirectory and want to delete this file on componentWillUnmount. Below is the code snippet I am trying but no luck.

useEffect(() => () => { FileSystem.deleteAsync(fileUri) })

In another screen, when I check for the file info

const { exists } = await FileSystem.getInfoAsync(fileUri);

exists value is true

Isn’t it possible to delete a file on componentWillUnmount? How to delete cache files which are required only in the current screen and can be delete while moving to another screen?

I was taking fileUri from useState and it was throwing error.
Now, I am taking fileUri directly from cacheDirectory ${FileSystem.cacheDirectory}/file.pdf and it worked!!