Shrink SVGs and Avoid ID Conflicts with SVGO’s cleanupIDs Plugin
Use SVGO’s cleanupIDs plugin to shrink SVG files, remove duplicate IDs, and avoid CSS/JS conflicts when embedding multiple SVGs. Follow this step‑by‑step guide to configure, run, and verify the optimization safely.
22 May 2026, 16:10 UTC

Problem: ID Collisions and File Bloat in Embedded SVGs
When a web page embeds multiple SVG files, duplicate id attributes can cause CSS and JavaScript selectors to target the wrong element. Additionally, long, human‑readable IDs increase file size and make caching harder because identical content can still differ in the ID names. The cleanupIDs plugin in SVGO addresses both issues by removing unused IDs, shortening long ones, and rewriting internal references.
Desired Outcome
Produce a single, deterministic SVG file that:
- has no duplicate or unused
idattributes - uses short, unique IDs that reduce file size
- keeps all visual elements and internal
<use>references intact - does not break external CSS or JavaScript that previously referenced the original IDs
Prerequisites
- Node.js 20+ and npm installed.
- SVGO 3.x or newer. Install globally or locally:
npm install -g svgo - SVG file(s) you intend to optimize.
- Optional: a build script or task runner to automate the process.
Procedure
1. Verify the Current State
Before running any optimizer, note the original file size and check for duplicate IDs. A quick grep can reveal duplicates:
grep -o 'id="[^"]*"' input.svg | sort | uniq -d
If the list is empty, duplicate IDs are not an issue, but cleanupIDs will still shorten IDs.
2. Configure SVGO with cleanupIDs
SVGO 3.x exposes the plugin via the cleanupIDs option. Create a configuration file .svgo.yml in your project root:
cleanupIDs:
remove: true # remove all IDs that are not referenced inside the SVG
minify: true # shorten IDs to the shortest unique form
Both sub‑options default to true, but they are shown explicitly for clarity. If you need to preserve certain IDs, you can supply a preserve array of regular expressions.
3. Run the Optimizer
Execute SVGO from the command line, pointing at your input file and output file:
svgo -p cleanupIDs input.svg -o output.svg
Where:
-p cleanupIDsactivates the plugin.input.svgis the source file.-o output.svgwrites the optimized file.
Run with --verbose to see which IDs were removed or renamed.
4. Update External References (If Needed)
Because cleanupIDs rewrites internal references, any external CSS or JavaScript that targeted the original IDs must be updated. A pragmatic approach is to use a post‑build step that scans the optimized SVG for id attributes and generates a mapping file. Example:
node -e "
const fs=require('fs');
const xml=require('fast-xml-parser');
const input=fs.readFileSync('output.svg','utf8');
const obj=xml.parse(input,{ignoreAttributes:false});
// walk obj to collect id mappings (original vs new)
// ...
" > id-mapping.json
Once you have the mapping, update your CSS/JS accordingly.
5. Verify the Result
- File Size Check: Compare
input.svgandoutput.svgsizes. A reduction of 5–20% is typical for complex icons. - Visual Integrity: Open both files in a browser and confirm that the rendered image is unchanged.
- Reference Integrity: If you have unit tests that query elements by ID, run them against
output.svgand ensure all expected elements are found. - Cacheability: Because
cleanupIDsproduces deterministic output, identical input yields identical output. Use a hashing tool to verify that two identical source files produce the same hash after optimization.
Recovery Options
- Rollback: Keep a copy of the original SVG. If visual or functional regressions appear, replace
output.svgwithinput.svg. - Selective Preservation: If certain IDs must remain unchanged, edit the
.svgo.ymlto add apreservelist. Example:cleanupIDs: preserve: - '^icon-' - Re‑optimize: If you need to re‑apply cleanupIDs after adding new IDs, run the same command again; the plugin will handle new duplicates.
Limitations and When to Skip cleanupIDs
- Very small SVGs (<10 KB) may see negligible size savings, and the plugin’s overhead could slightly increase the file.
- If your SVG is heavily manipulated by scripts that rely on specific ID names, you must update those scripts or disable the plugin.
- In rare cases,
cleanupIDsmay remove an ID that is referenced by an external script that runs after the SVG loads. Test such scripts thoroughly.
Practical Checklist
| Step | Action | Check |
|---|---|---|
| 1 | Run grep for duplicate IDs. | No duplicates found. |
| 2 | Configure .svgo.yml. | File exists and contains cleanupIDs block. |
| 3 | Execute svgo command. | output.svg created without errors. |
| 4 | Update external references if needed. | Mapping file generated. |
| 5 | Verify size, visual, and functional integrity. | All tests pass. |
Conclusion
SVGO’s cleanupIDs plugin is a reliable tool for shrinking SVG files and preventing ID collisions when many icons are embedded on a page. By configuring the plugin, updating any external references, and verifying the output, you can achieve smaller, cache‑friendly SVGs without sacrificing visual fidelity.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.