Sub topic 4
Keeping files out of your project with .forceignore
This chapter assumes you can run sf commands, you know the project layout from the earlier chapters, and you have used .gitignore before. It covers the .forceignore file: what it controls, how its patterns work, and the mistakes that cause confusing retrieve and deploy results.
What .forceignore does
.forceignore is a text file in the root of your Salesforce DX project, next to sfdx-project.json. It lists files and folders that the CLI should treat as if they did not exist. The CLI reads it whenever it works out which local files take part in an operation:
sf project deploy startandsf project deploy validateskip matching files.sf project retrieve startdoes not write matching files into your project.- Source tracking leaves matching files out of its change list, so ignored files never show up as local or remote changes.
.forceignore is not .gitignore. Git ignores files for version control. The CLI ignores files for org operations. A file can be in one list, both, or neither, and you usually need to maintain both files.
The default file
A new project from sf project generate includes a .forceignore like this:
# List files or directories below to ignore them when running
# deploy, retrieve, and source tracking commands.
package.xml
# LWC configuration files
**/jsconfig.json
**/.eslintrc.json
# LWC Jest
**/__tests__/**Each entry has a reason:
package.xmlis a manifest that describes a deployment. It is not metadata.jsconfig.jsonand.eslintrc.jsonhelp your editor and linter. Salesforce would reject them inside a Lightning web component bundle.__tests__folders hold Jest unit tests. Jest runs locally and the org cannot store these files.
If you delete these lines, you may see errors the next time you deploy a Lightning web component.
Pattern syntax
The syntax follows .gitignore rules, and paths are relative to the project root.
| Pattern | Matches |
|---|---|
| `# text` | A comment. Blank lines are also skipped. |
| `*.txt` | Any `.txt` file in any folder. |
| `**/profiles/**` | Everything inside any folder named `profiles`, at any depth. |
| `force-app/main/default/staticresources/` | A directory. The trailing slash restricts the match to directories. |
| `/scripts` | Only the `scripts` entry at the project root. A leading slash anchors the pattern. |
| `!keep.cls` | Negation. It re-includes a file that an earlier pattern excluded. |
Three rules prevent most surprises:
- Order matters. The last matching line wins. Put a
!exception after the broader pattern it overrides. - A negation cannot rescue a file inside an excluded directory. If you exclude
**/profiles/**, a later!**/profiles/Admin.profile-meta.xmlhas no effect, because the CLI never looks inside the directory. Exclude the files with**/profiles/*instead, and then add the exception. - Use forward slashes, even on Windows.
Metadata files come in pairs
Many components store content and configuration in two files. An Apex class AccountService is made of AccountService.cls and AccountService.cls-meta.xml. If you ignore only one of them, the CLI sees half a component and fails or behaves oddly.
Match both files with a trailing wildcard:
**/classes/LegacyImporter.cls*
**/classes/LegacyImporterTest.cls*The same applies to triggers (.trigger and .trigger-meta.xml) and to other source-format files that have a -meta.xml companion.
A realistic example
This .forceignore extends the defaults for a team that manages some metadata outside the CLI:
package.xml
**/jsconfig.json
**/.eslintrc.json
**/__tests__/**
# Profiles are managed by a separate release process
**/profiles/*
!**/profiles/Admin.profile-meta.xml
# Legacy code that must stay in the org but out of this repo
**/classes/LegacyImporter.cls*
# Generated by local tooling
**/*.log
.sfdx/
.sf/Walking through it:
**/profiles/*excludes every profile, and the next line adds back only theAdminprofile. This works because the first pattern excludes files and not the directory.LegacyImporter.cls*covers both files of the class..sf/and.sfdx/are local CLI state folders that sit outside your source directories. They are shown here only as an example of a tooling pattern. If your editor writes other generated files into a package directory, ignore them the same way.
How the CLI decides
Checking that your patterns work
After you edit .forceignore, confirm the result before a real deploy. Run a preview and look at which components appear:
sf project deploy preview --source-dir force-app --target-org my-sandboxComponents that your patterns exclude should not appear in the list of changes to deploy. For a stronger check, use the dry run from earlier in this tutorial:
sf project deploy start --source-dir force-app --target-org my-sandbox --dry-runYou can also check source tracking. An ignored file that you edit should not change the output of sf project deploy preview.
Behavior to know about
Ignoring affects retrieve too. If you ignore **/profiles/* and then run sf project retrieve start --metadata Profile, the retrieved profile files are not written to your project. This surprises people who assume ignore rules only protect deployments. When a retrieve "succeeds" but nothing appears locally, check .forceignore first.
Ignoring does not delete anything. A file that is already in your project and later matches a pattern stays on disk. The CLI just stops considering it. Likewise, ignoring a component does not remove it from the org. To remove components, use destructive changes, which a later chapter covers.
Dependencies can still fail. If you ignore a custom field that a layout or Apex class references, a deploy to a different org can fail with "no such column" or "invalid reference" errors. The ignore rule hides the field from the CLI, but the other components still need it. Ignore related components together, or confirm that the target org already has the field.
A manifest does not override the ignore file. If package.xml names ApexClass:LegacyImporter but .forceignore excludes its files, the CLI does not deploy that class. Listing a component and ignoring it conflict, and the ignore file wins.
The file belongs in version control. Commit .forceignore so every developer and your CI jobs apply the same rules. A pipeline that deploys without it can push files your developers never meant to send.
Common mistakes
- Ignoring
AccountService.clsbut notAccountService.cls-meta.xml. UseAccountService.cls*. - Placing the negation before the broad pattern. The later, broader pattern then wins and the exception has no effect.
- Excluding a whole directory and then expecting
!to bring back one file inside it. - Using backslashes in paths on Windows.
- Creating
.forceignorein a subfolder. Keep a single file in the project root. - Editing
.gitignorewhen the real cause is.forceignore, or the reverse.
Exercise
Your .forceignore contains **/profiles/** followed by !**/profiles/Admin.profile-meta.xml. The Admin profile is still never deployed or retrieved. Why, and how do you fix it?
Show answer
A negation cannot re-include a file when its parent directory is excluded. **/profiles/** excludes the whole profiles directory, so the CLI never reaches the Admin file. Change the first pattern to **/profiles/* so it matches the files and not the directory. Keep the !**/profiles/Admin.profile-meta.xml line after it. Then run sf project deploy preview to confirm Admin appears and the other profiles do not.
Exercise
You want to keep the Apex class LegacyImporter out of every deploy and retrieve. Write the .forceignore line, and explain why **/classes/LegacyImporter.cls alone is not enough.
Show answer
Use **/classes/LegacyImporter.cls*. An Apex class has two files, LegacyImporter.cls and LegacyImporter.cls-meta.xml. The pattern without the trailing * excludes only the first, which leaves a metadata file with no source file. The wildcard matches both.