Below, you query for an instance with an `id` of `"123"`, and then delete it, if found:
```java
QueryOptions queryOptions = null;
try {
queryOptions = Where.identifier(Post.class, "123");
} catch (AmplifyException e) {
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
}
if (queryOptions != null) {
Amplify.DataStore.query(Post.class, queryOptions,
matches -> {
if (matches.hasNext()) {
Post post = matches.next();
Amplify.DataStore.delete(post,
deleted -> Log.i("MyAmplifyApp", "Deleted a post."),
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
);
}
},
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
);
}
```
```kotlin
Amplify.DataStore.query(Post::class.java, Where.identifier(Post::class.java, "123"),
{ matches ->
if (matches.hasNext()) {
val post = matches.next()
Amplify.DataStore.delete(post,
{ Log.i("MyAmplifyApp", "Deleted a post.") },
{ Log.e("MyAmplifyApp", "Delete failed.", it) }
)
}
},
{ Log.e("MyAmplifyApp", "Query failed.", it) }
)
```
```kotlin
Amplify.DataStore.query(Post::class, Where.identifier(Post::class.java, "123"))
.catch { Log.e("MyAmplifyApp", "Query failed", it) }
.onEach { Amplify.DataStore.delete(it) }
.catch { Log.e("MyAmplifyApp", "Delete failed", it) }
.collect { Log.i("MyAmplifyApp", "Deleted a post") }
```
```java
QueryOptions queryOptions = null;
try {
queryOptions = Where.identifier(Post.class, "123");
} catch (AmplifyException e) {
Log.e("MyAmplifyApp", "Failed to construct QueryOptions with provided values for Where.identifier", e);
}
if (queryOptions != null) {
RxAmplify.DataStore.query(Post.class, queryOptions)
.flatMapCompletable(RxAmplify.DataStore::delete)
.subscribe(
() -> Log.i("MyAmplifyApp", "Deleted a post."),
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
);
}
```
To conditionally delete an instance, pass an instance and a predicate into `Amplify.DataStore.delete()`.
In the example below, posts are deleted only if their `rating` is greater than `4`:
```java
Amplify.DataStore.query(Post.class,
matches -> {
if (matches.hasNext()) {
Post post = matches.next();
Amplify.DataStore.delete(post, Where.matches(Post.RATING.gt(4)).getQueryPredicate(),
deleted -> Log.i("MyAmplifyApp", "Deleted a post."),
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
);
}
},
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
);
```
```kotlin
Amplify.DataStore.query(Post::class.java,
{ matches ->
if (matches.hasNext()) {
val post = matches.next()
Amplify.DataStore.delete(post, Where.matches(Post.RATING.gt(4)).queryPredicate,
{ Log.i("MyAmplifyApp", "Deleted a post.") },
{ Log.e("MyAmplifyApp", "Delete failed.", it) }
)
}
},
{ Log.e("MyAmplifyApp", "Query failed.", it) }
)
```
```kotlin
Amplify.DataStore.query(Post::class)
.catch { Log.e("MyAmplifyApp", "Query failed", it) }
.onEach { Amplify.DataStore.delete(it, Where.matches(Post.RATING.gt(4)).queryPredicate) }
.catch { Log.e("MyAmplifyApp", "Delete failed", it) }
.collect { Log.i("MyAmplifyApp", "Deleted a post") }
```
```java
RxAmplify.DataStore.query(
Post.class,
Where.matches(Post.RATING.gt(4)))
.subscribe(
post -> RxAmplify.DataStore.delete(post, Where.matches(Post.RATING.gt(4)).getQueryPredicate())
.subscribe(
() -> Log.i("MyAmplifyApp", "Deleted a post."),
failure -> Log.e("MyAmplifyApp", "Delete failed.", failure)
),
failure -> Log.e("MyAmplifyApp", "Query failed.", failure)
);
```