diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index 665bd4eb3aba865c458186ca4ccb374b8b14accc..fd03a8ec03516b153cf9a3cee26d4036039cb0cd 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -3232,7 +3232,9 @@ static int leave_voicemail(struct ast_channel *chan, char *ext, struct leave_vm_
 						ast_destroy_realtime("voicemail_data", "id", tmpid, NULL);
 					}
 				} else {
+#ifndef IMAP_STORAGE
 					msgnum = last_message_index(vmu, dir) + 1;
+#endif
 					make_file(fn, sizeof(fn), dir, msgnum);
 
 					/* assign a variable with the name of the voicemail file */