{"record":{"id":"fab84da81fdb6dd7","repo":"crowdsecurity/crowdsec","slug":"while-removing-file-w","errorCode":null,"errorMessage":"while removing file: %w","messagePattern":"while removing file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/hubops/purge.go","lineNumber":64,"sourceCode":"\tif !i.State.IsDownloaded() {\n\t\treturn false, nil\n\t}\n\n\treturn true, nil\n}\n\nfunc (c *PurgeCommand) Run(_ context.Context, _ *ActionPlan) error {\n\ti := c.Item\n\n\tfmt.Fprintln(os.Stdout, \"purging \" + colorizeItemName(i.FQName()))\n\n\tif err := os.Remove(i.State.DownloadPath); err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\ti.State.DownloadPath = \"\"\n\t\t\treturn nil\n\t\t}\n\n\t\treturn fmt.Errorf(\"while removing file: %w\", err)\n\t}\n\n\ti.State.DownloadPath = \"\"\n\ti.State.Tainted = false\n\ti.State.UpToDate = false\n\n\treturn nil\n}\n\nfunc (*PurgeCommand) OperationType() string {\n\treturn \"purge (delete source)\"\n}\n\nfunc (c *PurgeCommand) ItemType() string {\n\treturn c.Item.Type\n}\n\nfunc (c *PurgeCommand) Detail() string {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/hubops/purge.go#L46-L82","documentation":"PurgeCommand.Run deletes the item's downloaded file with os.Remove and wraps any failure other than not-exist. It means the OS refused to unlink the downloaded hub item's file at State.DownloadPath — not that the item was missing (that case is swallowed and treated as already purged).","triggerScenarios":"PurgeCommand.Run on an item with a non-empty DownloadPath where os.Remove fails with something other than os.IsNotExist: e.g. EACCES on the file or parent directory, EBUSY/EPERM on platforms locking open files, or read-only filesystem.","commonSituations":"Running cscli as non-root while the hub files are root-owned; file still held open by a running crowdsec process on Windows; the data/ config directory is mounted read-only; permissions changed by a previous run as root.","solutions":["Re-run the command with sufficient privileges (sudo/admin) so the download path can be unlinked","Check permissions on the file and its parent directory (ls -l) and correct ownership","Stop any running crowdsec process holding the file open, then purge again","Verify the filesystem is writable and not mounted read-only"],"exampleFix":"// before\ncscli hub purge crowdsecurity/http-broken-links\n// after\nsudo cscli hub purge crowdsecurity/http-broken-links","handlingStrategy":"try-catch","validationCode":"if info, err := os.Stat(item.State.DownloadPath); err == nil {\n\tif info.IsDir() || !canWrite(filepath.Dir(item.State.DownloadPath)) {\n\t\t// elevate privileges or fix permissions before purging\n\t}\n}","typeGuard":null,"tryCatchPattern":"err := purgeCmd.Run(ctx, plan)\nvar pe *fs.PathError\nif errors.As(err, &pe) && errors.Is(pe.Err, fs.ErrPermission) {\n\t// retry with elevated privileges or fix ownership\n}","preventionTips":["Run hub management commands with the same user that owns the hub directory","Avoid running some cscli commands as root and others as a normal user (mixed ownership)","On Windows, close processes holding hub files open before purging"],"tags":["filesystem","permissions","file-delete"],"backgroundTag":"file-delete-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}