diff --git a/camel/camel-exchange-folder.c b/camel/camel-exchange-folder.c
index 0b670ae..53c0d21 100644
--- a/camel/camel-exchange-folder.c
+++ b/camel/camel-exchange-folder.c
@@ -83,11 +83,8 @@ exchange_folder_append_message_data (CamelFolder *folder,
        if (success) {
                stream_cache = camel_data_cache_add (
                        exch->cache, "cache", new_uid, NULL);
-               if (stream_cache) {
-                       camel_stream_write (stream_cache,
-                                           (gchar *) message->data,
-                                           message->len, cancellable, NULL);
-                       camel_stream_flush (stream_cache, cancellable, NULL);
+        if (stream_cache) {
+            if (camel_stream_write (stream_cache, (gchar *) message->data, message->len, cancellable, NULL) <= 0 || camel_stream_flush (stream_cache, cancellable, NULL) == -1) camel_data_cache_remove (exch->cache, "cache", new_uid, NULL);
                        g_object_unref (stream_cache);
                }
                if (appended_uid)
@@ -119,8 +116,19 @@ exchange_folder_get_message_data (CamelFolder *folder,
        exch = CAMEL_EXCHANGE_FOLDER (folder);
        store = CAMEL_EXCHANGE_STORE (parent_store);
 
-       stream = camel_data_cache_get (exch->cache, "cache", uid, NULL);
-       if (stream) {
+    stream = camel_data_cache_get (exch->cache, "cache", uid, NULL);
+    if (stream) {
+        CamelStream *null_stream = camel_stream_null_new ();
+
+        camel_stream_reset (stream, NULL);
+        if (camel_stream_write_to_stream (stream, null_stream, cancellable, NULL) <= 0) {
+            stream = NULL;
+        }
+
+        g_object_unref (null_stream);
+    }
+
+    if (stream) {
                ba = g_byte_array_new ();
                stream_mem = camel_stream_mem_new ();
                camel_stream_mem_set_byte_array (CAMEL_STREAM_MEM (stream_mem), ba);
@@ -150,8 +158,10 @@ exchange_folder_get_message_data (CamelFolder *folder,
                return NULL;
        }
 
-       camel_stream_write (stream, (gchar *) ba->data, ba->len, cancellable, NULL);
-       camel_stream_flush (stream, cancellable, NULL);
+    if (camel_stream_write (stream, (gchar *) ba->data, ba->len, cancellable, NULL) <= 0 ||
+        camel_stream_flush (stream, cancellable, NULL) == -1)
+            camel_data_cache_remove (exch->cache, "cache", uid, NULL);
+
        g_object_unref (stream);
 
        return ba;

