deleteItemAsync
- React Native course
- Advanced
- Lesson 40/83
- 5 min
Learn secure deletion with deleteItemAsync.
deleteItemAsync
To delete a value from the secure store you use SecureStore.deleteItemAsync. The function takes one parameter: the key. If the key exists it deletes the value; if not, it does not error. The function is asynchronous so it is awaited.
Example: await SecureStore.deleteItemAsync('token'). This deletes the value of the 'token' key. It is used in the logout flow; when the user presses the 'Log out' button the token is deleted and the session does not persist on the next open. After deletion, reading with getItemAsync returns null. This confirms the session is closed.
Logout flow
Logout is the user's safe exit from the app. The flow consists of three steps: delete the token, clear the state, redirect the user to the login screen. You delete the token with await SecureStore.deleteItemAsync('token'); clear the user state with setUser(null); redirect to the login screen with navigation.replace('Login').
After deletion the token cannot be read with getItemAsync; so the session does not persist on the next open. State clearing makes the user info on the screen disappear immediately. The redirect makes the user return to the login screen. These three steps together form a safe logout.
Practice Delete the token
Complete the clearToken function. Call await SecureStore.deleteItemAsync('token').
Starter code
import * as SecureStore from 'expo-secure-store';
export async function clearToken() {
// delete the token from the secure store
}Sample solution
await SecureStore.deleteItemAsync('token');Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Write the delete function
await SecureStore.____('token');
await SecureStore.deleteItemAsync('token');
Explanation: deleteItemAsync deletes from the secure store.
2 Match each step to its purpose
| Term | Meaning |
|---|---|
deleteItemAsync |
Deletes the token |
setUser(null) |
Clears the state |
navigation.replace |
Redirects to login |
getItemAsync |
Deleted token returns null |
3 What does getItemAsync return after deletion?
await SecureStore.deleteItemAsync('token');
const t = await SecureStore.getItemAsync('token');- The old value
- null Correct answer
- Throws an error
- Empty string
Explanation: After deletion getItemAsync returns null.