diff --git a/ecrire/optimiser.php3 b/ecrire/optimiser.php3
index 08aeee50ab776f862e186d4e50a4f9e3a638d383..fda4c2ad02f609ea010268d662a77b45518f7bfc 100644
--- a/ecrire/optimiser.php3
+++ b/ecrire/optimiser.php3
@@ -171,21 +171,51 @@ function optimiser_base() {
 	$result = spip_query($query);
 	while ($row = spip_fetch_array($result)) $mots[] = $row['id_mot'];
 
-	if ($mots) {
-		$mots = join(",", $mots);
+	
+	// 
+	// Documents
+	//
 
-		$query = "DELETE FROM spip_mots_articles WHERE id_mot NOT IN ($mots)";
-		spip_query($query);
-		$query = "DELETE FROM spip_mots_breves WHERE id_mot NOT IN ($mots)";
-		spip_query($query);
-		$query = "DELETE FROM spip_mots_forum WHERE id_mot NOT IN ($mots)";
-		spip_query($query);
-		$query = "DELETE FROM spip_mots_rubriques WHERE id_mot NOT IN ($mots)";
-		spip_query($query);
-		$query = "DELETE FROM spip_mots_syndic WHERE id_mot NOT IN ($mots)";
-		spip_query($query);
+	$query = "SELECT id_document FROM spip_documents_articles";
+	$result = spip_query($query);
+	while ($row = spip_fetch_array($result)) $documents[] = $row['id_document'];
+	$query = "SELECT id_document FROM spip_documents_rubriques";
+	$result = spip_query($query);
+	while ($row = spip_fetch_array($result)) $documents[] = $row['id_document'];
+	$query = "SELECT id_document FROM spip_documents_breves";
+	$result = spip_query($query);
+	while ($row = spip_fetch_array($result)) $documents[] = $row['id_document'];
+	
+	if ($documents) {
+		$documents = join(",", $documents);
+
+		$query = "SELECT id_document, fichier FROM spip_documents WHERE id_document NOT IN ($documents)";
+		$result = spip_query($query);
+		while ($row = spip_fetch_array($result)) {
+			$documents_poubelle[] = $row['id_document'];
+			$fichiers_poubelle[] = $row['fichier'];
+		}
+
+		if ($documents_poubelle) {
+			$documents_poubelle = join(",", $documents_poubelle);
+
+			$fichier = $GLOBALS['dir_ecrire'].'data/.poubelle';
+			if (!$f = @fopen($fichier, 'a')) {
+				spip_log("impossible d'ecrire dans $fichier !");
+				@unlink($fichier);	// on essaie de forcer
+				$f = @fopen($fichier, 'a');
+			}
+			spip_log("mise a la poubelle : ".join(", ", $fichiers_poubelle));
+			$ok = fputs($f, join("\n", $fichiers_poubelle)."\n");
+			fclose($f);
+			if ($ok) {
+				$query = "DELETE FROM spip_documents WHERE id_document IN ($documents_poubelle)";
+				spip_query($query);
+			}
+		}
 	}
 
+
 	//
 	// Forums
 	//
diff --git a/inc-public-global.php3 b/inc-public-global.php3
index a2a11f8b722d063088f226840fde3c6c10a4b328..ccb3695b44c68ba2d723792f9891fbd70da1f927 100644
--- a/inc-public-global.php3
+++ b/inc-public-global.php3
@@ -348,6 +348,37 @@ if (lire_meta("activer_statistiques") != "non") {
 }
 
 
+//
+// Effacement de la poubelle (documents supprimes)
+//
+
+if (file_exists($fichier_poubelle = "ecrire/data/.poubelle")) {
+	if (timeout('poubelle')) {
+		if ($s = sizeof($suite = file($fichier_poubelle))) {
+			$s = $suite[$n = rand(0, $s)];
+			$s = trim($s);
+			include_ecrire("inc_connect.php3");
+			if ($db_ok) {
+				// Verifier qu'on peut vraiment effacer le fichier...
+				$query = "SELECT id_document FROM spip_documents WHERE fichier='$s'";
+				$result = spip_query($query);
+				if (spip_num_rows($result) OR !ereg('^IMG/', $s) OR strpos($s, '..')) {
+					spip_log("Tentative d'effacement interdit: $s");
+				}
+				else {
+					@unlink($s);
+				}
+				unset($suite[$n]);
+				$f = fopen($fichier_poubelle, 'wb');
+				fwrite($f, join("", $suite));
+				fclose($f);
+			}
+		}
+		else @unlink($fichier_poubelle);
+	}
+}
+
+
 //
 // Gerer l'indexation automatique
 //