SVG optimisation: what can be removed, what must stay, and how to measure it
An SVG exported from a design tool carries more than the drawing. It carries the editor's name, a metadata block, layer names, a preview thumbnail in some formats, namespace declarations, comments, and coordinates written to six decimal places. None of it is visible when the image renders, and all of it is downloaded by every visitor. Optimisation is the practice of removing that surplus while keeping the geometry, the styling and every identifier that something still points at.
The scale of the surplus is easy to underestimate. A 3 KB icon saved from a full editor can contain 1.5 KB of metadata and namespace declarations while the drawing itself is four path commands. In a set of forty icons, that surplus is repeated forty times on every page that uses the sprite. This is why optimisation is one of the few performance changes that costs nothing at runtime and cannot break layout.
What can be removed without changing the picture
- Comments and XML processing instructions, including the editor banner at the top of an export.
- The
<metadata>element with its RDF, Dublin Core and Creative Commons blocks. - Elements and attributes in an editor's own namespace, such as
inkscape:label,sodipodi:namedvieworsketch:type. - Namespace declarations that remain after those attributes are gone, and an
xmlns:xlinkthat no longer has anyxlink:attribute to serve. - Empty
<g>elements, groups that only wrap other groups without carrying a transform or a style, and empty<defs>blocks. - Whitespace-only text nodes between elements, the optional XML declaration and redundant root attributes such as
version.
What must stay
The rule for keeping something is reference, not appearance. An id may look unused but be the target of url(#gradient) in a style attribute, a clip-path, a mask, a filter, a marker, a SMIL animation or a <use href="#symbol">. Removing it turns a filled shape into a black silhouette or makes a clip disappear. Remove an id only after searching the file for #name and for the bare name in aria-labelledby.
<title> and <desc> are also structural, not decoration: they provide the accessible name that screen readers announce and the tooltip a browser shows. A <style> block may be the only place a class is defined. <defs> may look empty after cleanup but still hold the gradient or symbol the drawing references. When in doubt, keep the node and remove its unused attributes instead.
| Pass | Typical saving | Risk if done carelessly |
|---|---|---|
| Metadata, comments, RDF | 20–60% of an editor export | None — nothing references it |
| Editor namespaces and declarations | 2–10% | None once no prefixed attribute remains |
| Whitespace and XML declaration | 1–3% | None for rendering |
| Decimal rounding to 2 places | 5–20% | Sub-pixel shift; visible at 1 place on fine curves |
| Empty group merging | 1–5% | A group with a transform or opacity must not be unwrapped |
| Absolute/relative path rewriting | 3–15% | None — both forms describe the same points |
Measure bytes, not characters
A browser downloads bytes, and for SVG the byte count is not proportional to the character count once non-ASCII text, emoji or long ids appear. Measure the UTF-8 size of the file, then compare after gzip, because repeated tags and attribute names compress well and a file that looks 30% smaller may only be 12% smaller on the wire. A tool that reports both numbers is more honest than one that reports only the prettiest percentage.
Keep the original next to the optimised copy, and compare the rendered preview at the precision you intend to ship. At two decimals the difference for a 24 px icon is below one device pixel on a 2× display. At one decimal it can be visible on a curve with a large radius, which is why the optimizer warns when you choose one decimal place.
Optimise a copy, never the only copy. The passes are lossy only in coordinate precision, but the original file is the only way to check a visual difference later or to recover an id you removed by mistake.
A practical order of operations
Run the invisible-data passes first, then precision, then path rewriting, and stop. Reordering does not change the final size much, but it changes what you can verify: metadata removal is obviously safe, while precision needs a visual check. If you use a command-line tool in a build, pin its version and inspect the diff of one file before trusting it across a whole icon set.
Sources
Questions
Does optimisation change how the SVG looks?
Only if you ask it to. Removing metadata, comments, namespaces, empty groups and whitespace cannot affect rendering. Rounding coordinates can move a curve by a fraction of a pixel, which is why two decimals is the usual choice and one decimal should be checked in the preview before you ship it.
Can I delete every id attribute?
No. An id that looks unused may be the target of url(#name) in a fill or stroke, a clip-path, a mask, a filter, a marker, an animation or a use element. Remove an id only after searching the file for #name, and keep ids referenced from aria-labelledby or from script you intend to run.
Is a smaller SVG always faster to load?
Smaller is faster to download, but the bigger win for icons is often that a lean file gzips better and parses faster. A 3 KB icon reduced to 900 bytes saves bandwidth on every page view; a 90 KB illustration reduced by 10% saves less than converting it to a raster image at the size it is displayed.
Paste your own export into the optimizer to see each pass counted and the byte saving beside the output.
Open the optimizerWritten against the W3C SVG specifications, MDN and the React and WCAG documentation; every sample in the examples can be pasted into the optimizer linked above.
Back to the guides