diff --git a/.gitignore b/.gitignore
index d541310bbac00b6161d56fa6b07298db446e764c..a13fbeb175f3797acac63647a3c3532892982262 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,3 +16,4 @@ uploads/
 
 MisplaceAI/rules/migrations/0001_initial.py
 MisplaceAI/rules/migrations/__pycache__/0001_initial.cpython-310.pyc
+MisplaceAI/models
diff --git a/MisplaceAI/process_misplaced_manager/views.py b/MisplaceAI/process_misplaced_manager/views.py
index 190de693906adf028b2cfa105405eb7d6f4a37e6..67c7a8aaec31aea426fe09e0c67e760cf65ba7a0 100644
--- a/MisplaceAI/process_misplaced_manager/views.py
+++ b/MisplaceAI/process_misplaced_manager/views.py
@@ -279,34 +279,46 @@ def process_video_for_misplaced_objects(video_path, frame_interval, frame_delay)
 @api_view(['DELETE'])
 @permission_classes([IsAuthenticated])
 def delete_image(request, image_name):
-    print(f"Attempting to delete image: {image_name}")
-    # Construct the file path
-    file_path = os.path.join(settings.MEDIA_ROOT, image_name)
-    # Check if the file exists
-    if os.path.exists(file_path):
-        # Delete the file
-        os.remove(file_path)
-        print(f"Image {image_name} deleted successfully.")
-    else:
-        print(f"Image {image_name} not found. It may have already been deleted.")
-    return Response(status=status.HTTP_204_NO_CONTENT)
-
+    try:
+        print(f"Attempting to delete image: {image_name}")
+        # Construct the file path
+        file_path = os.path.join(settings.MEDIA_ROOT, image_name)
+        
+        # Check if the file exists
+        if os.path.exists(file_path):
+            # Delete the file
+            os.remove(file_path)
+            print(f"Image {image_name} deleted successfully.")
+            return Response(status=status.HTTP_204_NO_CONTENT)
+        else:
+            print(f"Image {image_name} not found.")
+            return Response({'error': 'Image not found'}, status=status.HTTP_404_NOT_FOUND)
+    except Exception as e:
+        print(f"Error deleting image {image_name}: {str(e)}")
+        return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
 
 
 @api_view(['DELETE'])
 @permission_classes([IsAuthenticated])
 def delete_video(request, video_name):
-    print(f"Attempting to delete video: {video_name}")
-    video_path = os.path.join(settings.MEDIA_ROOT, 'videos', video_name)
-    if os.path.exists(video_path):
-        os.remove(video_path)
-        print(f"Video {video_name} deleted successfully.")
-    else:
-        print(f"Video {video_name} not found. It may have already been deleted.")
-    return Response(status=status.HTTP_204_NO_CONTENT)
+    try:
+        print(f"Attempting to delete video: {video_name}")
+        video_path = os.path.join(settings.MEDIA_ROOT, 'videos', video_name)
+        if os.path.exists(video_path):
+            os.remove(video_path)
+            print(f"Video {video_name} deleted successfully.")
+            return Response(status=status.HTTP_204_NO_CONTENT)
+        else:
+            print(f"Video {video_name} not found.")
+            return Response({'error': 'Video not found'}, status=status.HTTP_404_NOT_FOUND)
+    except Exception as e:
+        print(f"Error deleting video {video_name}: {str(e)}")
+        return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
 
 
+ 
+
 #################################################################################################
 ####################################### Download Results ########################################
 @api_view(['GET'])
diff --git a/frontend/src/pages/NormalDetection/NormalDetectionPage.js b/frontend/src/pages/NormalDetection/NormalDetectionPage.js
index 601d1198d11c9ac264718f60ebaabfb740805923..1c18a9d7bc56d49afad4fe30fc8307d925bdd271 100644
--- a/frontend/src/pages/NormalDetection/NormalDetectionPage.js
+++ b/frontend/src/pages/NormalDetection/NormalDetectionPage.js
@@ -33,7 +33,7 @@ const NormalDetectionPage = () => {
 
     useEffect(() => {
         const handleBeforeUnload = async () => {
-            console.log('Navigation event detected:');
+            console.log('Navigation or route change detected:');
             if (imageName) {
                 console.log('Image name detected:', imageName);
                 try {
@@ -46,28 +46,9 @@ const NormalDetectionPage = () => {
         };
 
         window.addEventListener('beforeunload', handleBeforeUnload);
-
         return () => {
             window.removeEventListener('beforeunload', handleBeforeUnload);
-            handleBeforeUnload(); // Trigger the cleanup function manually
-        };
-    }, [imageName]);
-
-    useEffect(() => {
-        const handleRouteChange = async () => {
-            console.log('Route change detected:');
-            if (imageName) {
-                try {
-                    const response = await deleteImageByName(imageName);
-                    console.log('Delete image response:', response);
-                } catch (error) {
-                    console.error('Error deleting image:', error);
-                }
-            }
-        };
-
-        return () => {
-            handleRouteChange();
+            handleBeforeUnload();
         };
     }, [imageName, location]);
 
diff --git a/frontend/src/pages/Video/VideoDetectionPage.js b/frontend/src/pages/Video/VideoDetectionPage.js
index fb43528c688ba050ef9d4ab1d2cdd467022f7e4b..eead70a85a29ca09f146cf702a5eff83ca57b829 100644
--- a/frontend/src/pages/Video/VideoDetectionPage.js
+++ b/frontend/src/pages/Video/VideoDetectionPage.js
@@ -34,10 +34,10 @@ const VideoDetectionPage = () => {
     }, []);
 
     useEffect(() => {
-        const handleBeforeUnload = async () => {
+        const handleBeforeUnloadOrRouteChange = async () => {
             if (!isDeleting && annotatedVideoName) {
                 setIsDeleting(true);
-                console.log('Navigation event detected:');
+                console.log('Navigation or route change detected:');
                 console.log('Annotated video name detected:', annotatedVideoName);
                 try {
                     const response = await deleteVideo(annotatedVideoName);
@@ -49,31 +49,10 @@ const VideoDetectionPage = () => {
             }
         };
 
-        window.addEventListener('beforeunload', handleBeforeUnload);
-
-        return () => {
-            window.removeEventListener('beforeunload', handleBeforeUnload);
-            handleBeforeUnload();
-        };
-    }, [annotatedVideoName, isDeleting]);
-
-    useEffect(() => {
-        const handleRouteChange = async () => {
-            if (!isDeleting && annotatedVideoName) {
-                setIsDeleting(true);
-                console.log('Route change detected:');
-                try {
-                    const response = await deleteVideo(annotatedVideoName);
-                    console.log('Delete video response:', response);
-                } catch (error) {
-                    console.error('Error deleting video:', error);
-                }
-                setIsDeleting(false);
-            }
-        };
-
+        window.addEventListener('beforeunload', handleBeforeUnloadOrRouteChange);
         return () => {
-            handleRouteChange();
+            window.removeEventListener('beforeunload', handleBeforeUnloadOrRouteChange);
+            handleBeforeUnloadOrRouteChange();
         };
     }, [annotatedVideoName, location, isDeleting]);