How do I delete an object in ObjectBox?
Answer: Use box.remove(id).
- If an object with that ID exists, it will be deleted and the method returns
true. - If no object exists with that ID, it returns
false. - For deleting multiple objects efficiently, use
removeMany()or an equivalent bulk operation.
Purpose
The box.remove() method deletes a single object from the database based on its unique ID.
It is transactional and ensures that either the object is removed successfully or the operation has no effect.
Code Examples
- Java
- Kotlin
- Swift
- Dart
- Python
Java example
Box<User> userBox = store.boxFor(User.class);
long userIdToDelete = 1;
boolean wasRemoved = userBox.remove(userIdToDelete);
if (wasRemoved) {
System.out.println("Successfully removed user with ID " + userIdToDelete);
} else {
System.out.println("Could not find user with ID " + userIdToDelete + " to remove.");
}
Kotlin example
val userBox = store.boxFor(User::class.java)
val userIdToDelete = 1L
val wasRemoved = userBox.remove(userIdToDelete)
if (wasRemoved) {
println("Successfully removed user with ID $userIdToDelete")
} else {
println("Could not find user with ID $userIdToDelete to remove.")
}
Swift example
do {
let userBox = store.box(for: User.self)
let userIdToDelete: Id<User> = 1
let wasRemoved = try userBox.remove(userIdToDelete)
if wasRemoved {
print("Successfully removed user with ID \(userIdToDelete)")
} else {
print("Could not find user with ID \(userIdToDelete) to remove.")
}
} catch {
print("An error occurred: \(error)")
}
Dart example
final userBox = store.box<User>();
final userIdToDelete = 1;
final wasRemoved = userBox.remove(userIdToDelete);
if (wasRemoved) {
print('Successfully removed user with ID $userIdToDelete');
} else {
print('Could not find user with ID $userIdToDelete to remove.');
}
Python example
user_box = store.box(User)
user_id_to_delete = 1
was_removed = user_box.remove(user_id_to_delete)
if was_removed:
print(f"Successfully removed user with ID {user_id_to_delete}")
else:
print(f"Could not find user with ID {user_id_to_delete} to remove.")
Edge Cases & Pitfalls
- Non-existent ID: Returns
false(does not throw an exception). - Invalid ID (0 or negative): Treated as non-existent, returns
false. - Bulk deletes: Use
removeMany()for performance when removing multiple IDs. - Remove by object reference: Some bindings allow
remove(object)in addition toremove(id). - Transactions: Removing multiple objects can be wrapped in a single transaction for atomicity.
See Also
- box.put() – Inserting and updating objects
- box.get() – Fetching objects
- Transactions – Ensuring atomicity and consistency