Clean Up a Document

Sometimes you may need to remove unused or duplicate information to reduce the size of the output document and processing time.

While you can find and remove unused data, such as styles or lists, or duplicate information manually, it will be much more convenient to do this using features and capabilities provided by Aspose.Words.

The CleanupOptions class allows you to specify options for document cleaning. To remove duplicate styles or just unused styles or lists from the document, you can use the Cleanup method.

Remove Unused Information from a Document

You can use the UnusedStyles and UnusedBuiltinStyles properties to detect and remove styles that are marked as “unused”.

You can use the UnusedLists property to detect and remove lists and list definitions that are marked as “unused”.

The following code example shows how to remove only unused styles from a document:

Remove Duplicate Information from a Document

You can also use the DuplicateStyle property to substitute all duplicate styles with the original one and remove duplicates from a document.

The following code example shows how to remove duplicate styles from a document:


FAQ

  1. Q: How can I remove only the unused styles from a document?
    A: Create a CleanupOptions instance, set UnusedStyles to true, and call document.Cleanup(options). This removes styles that are not applied anywhere in the document while leaving used styles intact.

  2. Q: What is the difference between UnusedBuiltinStyles and UnusedStyles?
    A: UnusedBuiltinStyles targets the built‑in Word styles (e.g., Normal, Heading 1) that are not used, whereas UnusedStyles applies to custom styles defined in the document. Enabling both removes all unused style definitions.

  3. Q: Can I clean up duplicate styles in a single operation?
    A: Yes. Set the DuplicateStyle property of CleanupOptions to true and invoke document.Cleanup(options). Aspose.Words will replace duplicate style definitions with a single original style and delete the duplicates.